我有一个datagrid和另一个数据杂志嵌套在父数据杂题内。我需要将Child DataGrid的项目资源设置为Parent DataContext的属性。我已经尝试使用reactracteRce将儿童物品库来获取父datacontext。
XAML代码
<DataGrid Grid.Row ="1" x:Name="Grid1" ItemsSource="{Binding Path=md,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Categogy" Width="100" Binding="{Binding Path =CateName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTemplateColumn Header="Characterictics" Width="300">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding DataContext.list, RelativeSource={RelativeSource AncestorType=DataGrid}}">
<DataGridTextColumn Binding="{Binding DataContext.list.Name, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
ViewModel
Model m = new Model();
m.CateName = "CategoryName";
m.list.Add(new Characteristics() { Name="XX", Value="00"});
m.list.Add(new Characteristics() { Name = "XX", Value = "00" });
m.list.Add(new Characteristics() { Name = "XX", Value = "00" });
md.Add(m);
Model m1 = new Model();
m1.CateName = "CategoryName1";
m1.list.Add(new Characteristics() { Name = "XX", Value = "00" });
m1.list.Add(new Characteristics() { Name = "XX", Value = "00" });
m1.list.Add(new Characteristics() { Name = "XX", Value = "00" });
md.Add(m1);
list = new ObservableCollection<Characteristics>();
list.Add(new Characteristics() { Name = "XX", Value = "00" });
list.Add(new Characteristics() { Name = "XX", Value = "00" });
特征类具有属性名称和模型。
现在,我得到的例外,例如"物品收集必须在使用项目库之前为空"。
我不确定我在哪里做错了吗?有人可以帮助我正确地绑定儿童datagrid interce吗?
对于嵌套的datagrid放置DataGridTextColumn
中的 <DataGrid.Columns>
tag
<DataGrid ItemsSource="{Binding list}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>
否则,datagridtextcolumn被解释为项目,并导致ategrysource
的错误,还修复了绑定,他们不需要使用parent dataContext
我认为您可以尝试此
<DataGrid Grid.Row ="1" x:Name="Grid1" ItemsSource="{Binding Path=md,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Categogy" Width="100" Binding="{Binding Path =CateName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTemplateColumn Header="Characterictics" Width="600">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid AutoGenerateColumns="False" Height="100" ItemsSource="{Binding list}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
编辑:如果您希望从窗口的数据台上使用列表请更改为
的嵌套datarid <DataGrid AutoGenerateColumns="False" Height="100" ItemsSource="{Binding list,RelativeSource={RelativeSource AncestorType=Window}}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>
ViewModel:
public partial class MainWindow : Window
{
private ObservableCollection<Model> _md = new ObservableCollection<Model>();
public ObservableCollection<Model> md
{
get { return _md; }
set { _md = value; }
}
private ObservableCollection<Characteristics> _list;
public ObservableCollection<Characteristics> list
{
get { return _list; }
set { _list = value; }
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Model m = new Model();
m.CateName = "CategoryName";
m.list.Add(new Characteristics() { Name = "test Name", Value = "00" });
m.list.Add(new Characteristics() { Name = "TEst name1", Value = "00" });
m.list.Add(new Characteristics() { Name = "XX", Value = "00" });
md.Add(m);
Model m1 = new Model();
m1.CateName = "CategoryName1";
m1.list.Add(new Characteristics() { Name = "XX", Value = "00" });
m1.list.Add(new Characteristics() { Name = "XX", Value = "00" });
m1.list.Add(new Characteristics() { Name = "XX", Value = "00" });
md.Add(m1);
list = new ObservableCollection<Characteristics>();
list.Add(new Characteristics() { Name = "vali", Value = "00" });
list.Add(new Characteristics() { Name = "XX", Value = "00" });
}
}
public class Model
{
private string cateName;
public string CateName
{
get { return cateName; }
set { cateName = value; }
}
private List<Characteristics> _list = new List<Characteristics>();
public List<Characteristics> list
{
get { return _list; }
set { _list = value; }
}
}
public class Characteristics
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string value;
public string Value
{
get { return this.value; }
set { this.value = value; }
}
}