MS DataGrid (WPF):如何将组合框绑定到类?
这听起来像是一个微不足道的问题,但即使在 Stackflow 中,我也只发现绑定到一个简单的字符串集合。
我有一个具有两个属性 Name 和 Age 的 Parent 类。
我有一个 Child 类,它有两个属性 ChildName 和 ChildAge。
在 MVVM 模式中,我将这些属性公开到 ViewModel 中,此外,我还将 ObservableCollection Children 添加到 ParentViewModel 中,
因此 ParentViewModel 包含三个公开的属性:Name、Age 和 Children。
//Inside ParentViewModel
public ObservableCollection<ChildViewModel> Children
我的 Window.xaml 绑定到公开
public ObservableCollection<ParentViewModel> Parents { get; set; }
Datagrid 的 MainViewModel 定义如下:
<DataGrid ItemsSource="{Binding Parents}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
<DataGridComboBoxColumn Header="Children"
DisplayMemberPath="ChildName"
SelectedValueBinding="{Binding Children.ChildName}"
SelectedValuePath="ChildName"
SelectedItemBinding="{Binding Children}"
>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
虽然父母的姓名和年龄显示正确,但我没有看到填充的 Children 组合框。 我感到困惑和沮丧。请帮忙。 :)
This might sound like a trivial question, but even here in Stackflow I only have found binding to a simple string collection.
I have a Parent class with two properties Name and Age.
I have a Child class with two properties ChildName and ChildAge.
Within MVVM pattern I am exposing these properties into the ViewModels and additionally I am also adding into the ParentViewModel also an ObservableCollection Children
Therefore the ParentViewModel contains three exposed properties: Name, Age and Children.
//Inside ParentViewModel
public ObservableCollection<ChildViewModel> Children
My Window.xaml is bound to the MainViewModel that is exposing a
public ObservableCollection<ParentViewModel> Parents { get; set; }
The Datagrid is defined like this:
<DataGrid ItemsSource="{Binding Parents}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
<DataGridComboBoxColumn Header="Children"
DisplayMemberPath="ChildName"
SelectedValueBinding="{Binding Children.ChildName}"
SelectedValuePath="ChildName"
SelectedItemBinding="{Binding Children}"
>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
While Name and Age of parents show correctly, I dont see the Children combo box populated.
I am confused and frustrated. Please help. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
DataGridComboBoxColumn
的ItemsSource
属性设置为Children
。Set the
DataGridComboBoxColumn
'sItemsSource
property toChildren
.