如何使用 MVVM 将数据绑定到 DataGrid 中的 DataGridComboBoxColumn
这让我发疯。我有一个 DataGrid,其中有一个 DataGridComboBoxColumn,我希望用户能够使用它进行选择。这是我的网格的基本轮廓。
<DataGrid ItemsSource="{Binding GoalList}" DockPanel.Dock="Bottom" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn ItemsSource="{Binding LifeAreaList}" Header="Life Area"/>
<DataGrid.Columns>
</DataGrid>
DataGrid 绑定到 Goal 类型的对象集合。每个目标都有一个 LifeArea 类型的属性。每个 LifeArea 都有 LifeAreaId 和 Name 属性。
数据上下文包含可观察的目标集合:GoalList 和生活区域列表:LifeAreaList。我希望用户能够为目标选择不同的生活领域。另外,生活区域的名称也需要是显示的值。
编辑
解决方案是 DataGridComboBoxColumn 的 ItemsSource 必须设置为静态资源。另一种选择是通过代码设置 ItemsSource。
最后我有:
<DataGridComboBoxColumn x:Name="_lifeAreaComboBoxColumn" SelectedItemBinding="{Binding LifeArea}" DisplayMemberPath="Name" Header="Life Area">
在后面的代码中,我设置了 ItemsSource:
_lifeAreaComboBoxColumn.ItemsSource = LifeAreaDAL.GetLifeAreas();
当我有机会时,我会将其转换为 StaticResource。
This is driving me crazy. I have a DataGrid which has a DataGridComboBoxColumn which I want the user to be able to use to select from. This is the basic outline of my grid.
<DataGrid ItemsSource="{Binding GoalList}" DockPanel.Dock="Bottom" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn ItemsSource="{Binding LifeAreaList}" Header="Life Area"/>
<DataGrid.Columns>
</DataGrid>
The DataGrid is bound to a collection of objects of type Goal. Each Goal has a property of type LifeArea. Each LifeArea has the properties LifeAreaId and Name.
The data context contains an observable collection of Goals: GoalList and a list of Life Areas: LifeAreaList. I want the user to be able to select a different life area for a goal. Also the name of the life area needs to be the displayed value.
EDIT
The solution is that the ItemsSource for the DataGridComboBoxColumn has to be set as a static resource. Another option is to set the ItemsSource through code.
In the end I have:
<DataGridComboBoxColumn x:Name="_lifeAreaComboBoxColumn" SelectedItemBinding="{Binding LifeArea}" DisplayMemberPath="Name" Header="Life Area">
In the code behind I set the ItemsSource:
_lifeAreaComboBoxColumn.ItemsSource = LifeAreaDAL.GetLifeAreas();
When I get a chance I'll convert this to a StaticResource.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你需要做这样的事情(不要射击信使):
You need to do something like this (don't shoot the messenger):
除了绑定您的 SelectedItem 之外,我猜测您的 SelectedLifeArea 属性不是直接从 LifeAreaList 获取的,因此在比较两个值时,即使名称和 id 匹配,它们也会返回 false。您可能需要覆盖 LifeArea 对象的 .Equals 函数,以便在两个对象的 Id 匹配时返回 true
In addition to binding your SelectedItem, I am guessing that your SelectedLifeArea property is not obtained directly from LifeAreaList so when comparing the two values they are returning false, even if the name and id match. You probably need to overwrite the .Equals function of the LifeArea object to return true if the Ids of both objects match
Up 还可以使用 DataGridTemplateColumn,只需在其中放置一个 ComboBox,然后将适当的事件连接到它即可。
Up can also use a DataGridTemplateColumn and just put a ComboBox in it and then wire the appropriate events to it.