将 DataGridComboBoxColumn 绑定到一对多实体框架关系

发布于 2024-09-01 02:26:43 字数 1065 浏览 4 评论 0原文

我在模型中有两个表,一个表包含与另一个表以一对多关系相关的条目,例如:

Table User
  ID
  Name

Table Comments
  ID
  UserID
  Title
  Text

我想在 WPF 窗口中显示一个包含两列的数据网格,一个包含用户名的文本列,另一个包含用户名的文本列。带有组合框的列,显示用户所做的所有评论。

数据网格定义如下:

        <DataGrid AutoGenerateColumns="False" [layout options...] Name="dataGrid1" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/>
            <DataGridComboBoxColumn Header="Comments"
                SelectedValueBinding="{Binding Path=UserID}"
                SelectedValuePath="ID"
                DisplayMemberPath="Title"
                ItemsSource="{Binding Path=Comments}"
                />
        </DataGrid.Columns>
    </DataGrid>

在代码中,我像这样分配 DataContext:

dataGrid1.DataContext = entities.Users;

实体 User 有一个名为 Comments 的属性,该属性会导致用户所做的所有评论。查询正在返回数据并显示用户名,但未填充组合框。

可能这种方法是完全错误的,或者我只是在这里遗漏了一个非常简单的点,我愿意学习更好的方法来做到这一点。

谢谢

I have two tables in the model, one table contains entries related to the other table in a one to many relations, for example:

Table User
  ID
  Name

Table Comments
  ID
  UserID
  Title
  Text

I want to show a datagrid in a WPF window with two columns, one text column with the User name and another column with a combobox showing all the comments made by the user.

The datagrid definition is like this:

        <DataGrid AutoGenerateColumns="False" [layout options...] Name="dataGrid1" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/>
            <DataGridComboBoxColumn Header="Comments"
                SelectedValueBinding="{Binding Path=UserID}"
                SelectedValuePath="ID"
                DisplayMemberPath="Title"
                ItemsSource="{Binding Path=Comments}"
                />
        </DataGrid.Columns>
    </DataGrid>

in the code I assign the DataContext like this:

dataGrid1.DataContext = entities.Users;

The entity User has a property named Comments that leads to all the comments made by the user. The queries are returning data and the user names are shown but the combobox is not being filled.

May be the approach is totally wrong or I'm just missing a very simple point here, I'm opened to learn better methods to do this.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

青芜 2024-09-08 02:26:43

其实我已经用过你的方法了。它可以工作,但有一个警告:组合框只能显示给定对象的当前项目,如果它位于其允许值列表中(此处为 entities.Users)。

您会说“是的,但确实如此!我已经放入了整个 Users 列表”。遗憾的是,事实并非如此。 EF 中实体的默认比较不是基于 EntityKeys (我的猜测是它是默认比较,即引用比较),因此在对象中您有一个对一个对象的引用,在列表中 abd 您有一个对另一个的引用(两者具有相同的 EntityKey)。

我的解决方案是重写 User 类的比较函数,只需检查 ID==ID 即可。请注意,我并不是说这是最好的方法(例如,它可能会对其余代码产生不良后果),只是说它对我来说效果很好。

哦,一般建议不要将控件直接绑定到 IQueriables,而是绑定到 Execute 函数的结果(否则查询将针对数据库运行两次),请参阅 msdn 了解更多详细信息。

I have actually already used your approach. It works, but with a caveat: the combobox can only show the current item for a given object if it is in its list of allowed values (here entities.Users).

You'll say "yes, but it is! I've put in the whole Users list". Sadly, it isn't. The default comparison for entites in the EF is not based on EntityKeys (my guess is it's the default comparison, i.e. reference comparison), so in the object you've got a reference to one object, abd in the list you've got a reference to another (with the same EntityKey for both).

My solution is to override the comparison function for User class, with a simple check of ID==ID. Note I'm not saying this is the best approach (it may have unwanted consequences in the rest of your code, for example), just that it worked well for me.

Oh, and a general recommendation is not to bind directly controls to IQueriables, but to the result of the Execute function (or else the query will be run twice against the database), see msdn for more details.

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