简体   繁体   English

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

[英]Binding DataGridComboBoxColumn to a one to many entity framework relation

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. 我想在WPF窗口中显示一个带有两列的数据网格,一个带有用户名的文本列,另一个带有组合框的列,其中显示了用户所做的所有注释。

The datagrid definition is like this: datagrid定义如下:

        <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: 在代码中,我像这样分配DataContext:

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 谢谢

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 ). 它起作用了,但有一个警告:组合框只能显示给定对象的当前项(如果它在其允许值列表中)(在这里是entities.Users )。

You'll say "yes, but it is! I've put in the whole Users list". 您会说“是的,但是是!我已经放入整个Users列表”。 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, ie 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). EF中实体的默认比较不是基于EntityKeys(我猜这是默认比较,即引用比较),因此在对象中您具有对一个对象的引用,在列表中,您有一个abd引用另一个(两者具有相同的EntityKey)。

My solution is to override the comparison function for User class, with a simple check of ID==ID. 我的解决方案是通过简单检查ID == ID来覆盖User类的比较功能。 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. 哦,一般的建议是不要直接将控件绑定到IQueriables,而是绑定到Execute函数的结果(否则查询将对数据库运行两次),有关更多详细信息,请参见msdn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM