MS DataGrid (WPF):如何将组合框绑定到类?

发布于 2024-09-25 16:00:52 字数 1490 浏览 0 评论 0原文

这听起来像是一个微不足道的问题,但即使在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱成瘾 2024-10-02 16:00:52

DataGridComboBoxColumnItemsSource 属性设置为 Children

Set the DataGridComboBoxColumn's ItemsSource property to Children.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文