尝试找到一种方法来处理DataGrid的两个ObservableCollections。一个ObservableCollection应该包含DataGrids的集合,另一个是DataGrid的Rows集合。 在按钮上我添加了包含TextBoxes和DataGrid的Tabs。我直接在DataGrid中输入数据。在添加新选项卡时,我想在此选项卡中保存数据网格,并希望在新选项卡上有新的(没有数据)DataGrid,当我更改选项卡时,我想在每个具体的选项卡DataGrid中看到我输入的数据。这有可能吗?
ObservableCollection<DataGridsOBC>[0]
DataGrid 1
+---------------------------------------------+
+ | Column 1 | Column 2 | Column 3 |
+ | bind to | bind to | bind to |
+ | Property 1| Property 2| Property 3|
+---------------------------------------------|
ObservableCollection<RowsOBC>
+ Row 0 | ObservableCollection<RowsOBC>[0] |
+ Row 1 | ObservableCollection<RowsOBC>[1] |
+ Row 2 | ObservableCollection<RowsOBC>[2] |
+ ... | ObservableCollection<RowsOBC>[...] |
+---------------------------------------------+
ObservableCollection<DataGridsOBC>[1]
DataGrid 2
+---------------------------------------------+
+ | Column 1 | Column 2 | Column 3 |
+ | bind to | bind to | bind to |
+ | Property 1| Property 2| Property 3|
+---------------------------------------------|
ObservableCollection<RowsOBC>
+ Row 0 | ObservableCollection<RowsOBC>[0] |
+ Row 1 | ObservableCollection<RowsOBC>[1] |
+ Row 2 | ObservableCollection<RowsOBC>[2] |
+ ... | ObservableCollection<RowsOBC>[...] |
+---------------------------------------------+
如果它的可能性,它应该如何在xaml中看?
<DataGrid ItemsSource="{Binding }">
<DataGrid.Columns>
<DataGridTextColumn Header="Column 1" Binding="{Binding Property 1, Mode=TwoWay}"/>
<DataGridTextColumn Header="Column 2" Binding="{Binding Property 2, Mode=TwoWay}"/>
<DataGridTextColumn Header="Column 3" Binding="{Binding Property 3, Mode=TwoWay}"/>
</DataGrid.Columns>
</DataGrid
在代码背后?
public class DataGridsOBCViewModel : ViewModelBase
{
public ObservableCollection<RowsOBC> _Rows;
public ObservableCollection<RowsOBC> Rows
{
get { return _Rows != null ? _Rows : _Rows = new ObservableCollection<RowsOBC>(); }
set { _Rows = value; OnPropChange("Rows"); }
}
public DataGridsOBCViewModel()
{
}
}
public class RowsOBC : ViewModelBase
{
public ObservableCollection<Properties> _Items;
public ObservableCollection<Properties> Items
{
get { return _Items != null ? _Properties : _Items = new ObservableCollection<Properties>(); }
set { _Items = value; OnPropChange("Properties"); }
}
}
public class Properties: ViewModelBase
{
private string _Property1;
private string _Property2;
private string _Property3;
public string Property1
{
get { return _Property1; }
set { if (_Property1 != value) { _Property1 = value; OnPropChange("Property1"); } }
}
//............... etc
}