2012-07-13 48 views
1

有一個奇怪的問題,兩個WPF數據網格綁定到單獨的ObservableCollections。編輯一個數據網格改變另一個

這裏是我的XAML:

<Grid Name="gridShifts"> 
<Grid.ColumnDefinitions> 
    <ColumnDefinition/> 
    <ColumnDefinition/> 
</Grid.ColumnDefinitions> 
    <Custom:C1DataGrid Name="dgShift1" 
         HorizontalAlignment="Left" AutoGenerateColumns="False" CanUserAddRows="False" CanUserRemoveRows="False" CanUserReorderColumns="False" Grid.Column="0"> 
     <Custom:C1DataGrid.Columns> 
      <Custom:DataGridTextColumn Binding="{Binding Path=Type, Mode=TwoWay}" IsReadOnly="True" Header="Work Center"/> 
      <Custom:DataGridTextColumn Binding="{Binding Path=RegularSkill, Mode=TwoWay }" Header="Personnel"/> 
     </Custom:C1DataGrid.Columns> 
    </Custom:C1DataGrid> 
    <Custom:C1DataGrid Name="dgShift2" 
         HorizontalAlignment="Left" AutoGenerateColumns="False" CanUserAddRows="False" CanUserRemoveRows="False" CanUserReorderColumns="False" Grid.Column="1"> 
     <Custom:C1DataGrid.Columns> 
      <Custom:DataGridTextColumn Binding="{Binding Path=Type, Mode=TwoWay}" IsReadOnly="True" Header="Work Center"/> 
      <Custom:DataGridTextColumn Binding="{Binding Path=RegularSkill, Mode=TwoWay }" Header="Personnel"/> 
     </Custom:C1DataGrid.Columns> 
    </Custom:C1DataGrid> 
</Grid> 

這裏是我的代碼背後:

public partial class MainWindow : Window 
{ 
    AMMData.Manpower mp = new AMMData.Manpower(); 

    public MainWindow() 
    { 
     InitializeComponent(); 

     gridShifts.DataContext = mp; 
     dgShift1.ItemsSource = mp.WorkShifts[0].WCList; 
     dgShift2.ItemsSource = mp.WorkShifts[1].WCList; 

    } 
} 

編輯:這裏是人力資源類:

public enum WCSpecialty 
{ 
    Indirect, 
    Airframes, 
    AviationLifeSupport, 
    PeriodicMaintenance, 
    Electronics, 
    Electrical_Instruments, 
    Armaments, 
    Reconnaissance, 
    Line, 
    NA 
} 

public class Manpower : ComponentDataWrapper 
{ 
    #region Private Properties 

    private ObservableCollection<WCCollection> workShifts = new ObservableCollection<WCCollection>(); 

    #endregion 

    #region Public Properties 

    public ObservableCollection<WCCollection> WorkShifts { set { workShifts = value; NotifyPropertyChanged("WorkShifts"); } get { return workShifts; } } 

    #endregion 


    public Manpower() 
    { 
     Name = "New Work Center Structure"; 
     Description = "New Work Center Personnel Description"; 
     LastChanged = System.DateTime.Now; 

     var wcc1 = new AMMData.WCCollection(); 
     var wcc2 = new AMMData.WCCollection(); 

     var wc1 = new AMMData.WorkCenter { Type = AMMData.WCSpecialty.Indirect, RegularSkill = 0, MedSkill = 999, HighSkill = 999 }; 
     var wc2 = new AMMData.WorkCenter { Type = AMMData.WCSpecialty.Airframes, RegularSkill = 0, MedSkill = 999, HighSkill = 999 }; 
     var wc3 = new AMMData.WorkCenter { Type = AMMData.WCSpecialty.AviationLifeSupport, RegularSkill = 0, MedSkill = 999, HighSkill = 999 }; 
     var wc4 = new AMMData.WorkCenter { Type = AMMData.WCSpecialty.PeriodicMaintenance, RegularSkill = 0, MedSkill = 999, HighSkill = 999 }; 
     var wc5 = new AMMData.WorkCenter { Type = AMMData.WCSpecialty.Electronics, RegularSkill = 0, MedSkill = 999, HighSkill = 999 }; 
     var wc6 = new AMMData.WorkCenter { Type = AMMData.WCSpecialty.Electrical_Instruments, RegularSkill = 0, MedSkill = 999, HighSkill = 999 }; 
     var wc7 = new AMMData.WorkCenter { Type = AMMData.WCSpecialty.Armaments, RegularSkill = 0, MedSkill = 999, HighSkill = 999 }; 
     var wc8 = new AMMData.WorkCenter { Type = AMMData.WCSpecialty.Reconnaissance, RegularSkill = 0, MedSkill = 999, HighSkill = 999 }; 
     var wc9 = new AMMData.WorkCenter { Type = AMMData.WCSpecialty.Line, RegularSkill = 0, MedSkill = 999, HighSkill = 999 }; 
     var wc10 = new AMMData.WorkCenter { Type = AMMData.WCSpecialty.NA, RegularSkill = 0, MedSkill = 999, HighSkill = 999 }; 

     wcc1.WCList.Add(wc1); 
     wcc1.WCList.Add(wc2); 
     wcc1.WCList.Add(wc3); 
     wcc1.WCList.Add(wc4); 
     wcc1.WCList.Add(wc5); 
     wcc1.WCList.Add(wc6); 
     wcc1.WCList.Add(wc7); 
     wcc1.WCList.Add(wc8); 
     wcc1.WCList.Add(wc9); 
     wcc1.WCList.Add(wc10); 

     wcc2.WCList.Add(wc1); 
     wcc2.WCList.Add(wc2); 
     wcc2.WCList.Add(wc3); 
     wcc2.WCList.Add(wc4); 
     wcc2.WCList.Add(wc5); 
     wcc2.WCList.Add(wc6); 
     wcc2.WCList.Add(wc7); 
     wcc2.WCList.Add(wc8); 
     wcc2.WCList.Add(wc9); 
     wcc2.WCList.Add(wc10); 

     WorkShifts.Add(wcc1); 
     WorkShifts.Add(wcc2); 

    } 
} 

public class WCCollection : ComponentDataWrapper 
{ 
    private ObservableCollection<WorkCenter> wcList = new ObservableCollection<WorkCenter>(); 

    public ObservableCollection<WorkCenter> WCList { set { wcList = value; NotifyPropertyChanged("WCList"); } get { return wcList; } } 
} 

    public class WorkCenter : ComponentDataWrapper 
{ 
    #region private 

    private WCSpecialty type; 
    private int regularSkill; 
    private int highSkill; 
    private int medSkill; 

    #endregion 

    #region public 

    public WCSpecialty Type { set { type = value; NotifyPropertyChanged("Type"); } get { return type; } } 
    public int RegularSkill { set { regularSkill = value; NotifyPropertyChanged("RegularSkill"); } get { return regularSkill; } } 
    public int HighSkill { set { highSkill = value; NotifyPropertyChanged("HighSkill"); } get { return highSkill; } } 
    public int MedSkill { set { medSkill = value; NotifyPropertyChanged("MedSkill"); } get { return medSkill; } } 

    public int RegularWholePeople { get { return regularSkill/10; } } 

    #endregion 
} 

我的問題是,當我編輯一個數據網格,其他的值也會改變。我已經嘗試將兩個網格的datacontext設置爲它們各自的ObservableCollections,但同樣的事情發生。我覺得我對WPF數據綁定在這一點上的工作方式非常熟悉,但我完全無法理解這個問題。謝謝你的幫助。

+0

你可以顯示對象'mp'的代碼嗎? (另外,如果你在後臺代碼中設置'ItemsSource'而不是綁定它,你不需要在你的父網格上設置'DataContext') – Rachel 2012-07-13 16:48:20

+0

我添加了mp對象的代碼。我試圖在我的原始文章中簡潔。 – WasGoodDone 2012-07-13 17:00:28

回答

2

它看起來像您的項目被添加到您的ObservableCollections作爲參考,而不是價值。

這意味着兩個集合包含內存中的同一個對象的引用,所以在一個集合更新對象實際更新單個對象的引用,從而導致以及

+0

就是這樣。非常感謝你。 – WasGoodDone 2012-07-13 17:20:35

0

你已經通過了其他集合更新mp對象到DataGrid。因此,不需要使用後面的代碼來傳遞集合。

更新您的綁定是這樣的:

<Custom:DataGridTextColumn Binding="{Binding ElementName=gridShifts, Path=DataContex.WorkShifts[0].WCList Mode=TwoWay}" IsReadOnly="True" Header="Work Center"/> 

您還應該使用CollectionViewSource實例爲您數據源如下所示WPF’s CollectionViewSource。這是一個更清潔的設計,可能會在以後帶來優惠。

相關問題