WPF:无法将 DataGrid 绑定到枚举列表
我在绑定到枚举列表时遇到意外问题。绑定悄然失败,我无法解释原因。
此代码应该做的是创建一个 DataGrid,其中单元格模板作为填充有枚举选项的组合框,并将所选项目设置为列表元素。
对于将枚举元素作为 .Value 属性的可观察对象集合,此方法在其他地方效果很好。它似乎只是不喜欢枚举元素的可观察集合。
为了明确起见,这里有一些示例类:
public enum EquipmentEnum { EquipmentA, EquipmentB, EquipmentC }
public class EquipmentClass { public EquipmentEnum Value { get; set; } }
这个有效:
<DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding equipmentClassList}"> <!-- ObservableCollection<EquipmentClass> -->
<DataGrid.Columns>
<DataGridTemplateColumn Header="Equipment Used" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={local:Enumeration {x:Type EquipmentEnum}}}"
SelectedIndex="{Binding Path=Value Converter={StaticResource convertEnumValueToIndex}, Mode=TwoWay}"
DisplayMemberPath="Description"
IsEditable="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
这个不起作用:
<DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding equipmentEnumList}"> <!-- ObservableCollection<EquipmentEnum> -->
<DataGrid.Columns>
<DataGridTemplateColumn Header="Equipment Used" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={local:Enumeration {x:Type EquipmentEnum}}}"
SelectedIndex="{Binding Converter={StaticResource convertEnumValueToIndex}, Mode=TwoWay}"
DisplayMemberPath="Description"
IsEditable="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
只是一些注释:
local:Enumeration code> 是一个标记扩展,它只是以列表的形式提供枚举元素的描述。
提前致谢。
I'm having an unexpected problem binding to an enum list. The binding silently fails and I am at a loss to explain why.
What this code should do is create a DataGrid, with the cell template as a combobox populated with the enum choices, and the selected item set to the list element.
This approach works fine elsewhere, for observable collections of objects which have an enum element as their .Value property. It only seems to not like observable collections of enum element.
To be explicit, here are some example classes:
public enum EquipmentEnum { EquipmentA, EquipmentB, EquipmentC }
public class EquipmentClass { public EquipmentEnum Value { get; set; } }
This works:
<DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding equipmentClassList}"> <!-- ObservableCollection<EquipmentClass> -->
<DataGrid.Columns>
<DataGridTemplateColumn Header="Equipment Used" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={local:Enumeration {x:Type EquipmentEnum}}}"
SelectedIndex="{Binding Path=Value Converter={StaticResource convertEnumValueToIndex}, Mode=TwoWay}"
DisplayMemberPath="Description"
IsEditable="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
This does not work:
<DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding equipmentEnumList}"> <!-- ObservableCollection<EquipmentEnum> -->
<DataGrid.Columns>
<DataGridTemplateColumn Header="Equipment Used" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={local:Enumeration {x:Type EquipmentEnum}}}"
SelectedIndex="{Binding Converter={StaticResource convertEnumValueToIndex}, Mode=TwoWay}"
DisplayMemberPath="Description"
IsEditable="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Just some notes:
local:Enumeration
is a markup extension which simply provides the descriptions of the enum elements as a list.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绑定集合的项目将复制到单元格的 DataContext。您可以对 DataContext 引用的对象的属性使用双向绑定,因为您实际上正在更改绑定集合引用的同一对象(即 {Binding Path=Value, Mode=TwoWay} 将起作用)。
在第二个示例中,您尝试更改 DataContext 引用的对象,但 WPF 中没有类似的功能来“更新”绑定集合。
基本上,您只能更改 DataContext 引用的对象的状态。您无法更改它或绑定集合引用的对象。
The items of the bound collection are copied to the DataContext of the cells. You can use two-way bindings on properties of the object referenced by the DataContext, because you are effectively altering the same object referenced by the bound collection (i.e. {Binding Path=Value, Mode=TwoWay} will work).
In your second example, you are trying to alter the object referenced by DataContext, but there is no facility in WPF to "update" the bound collection like that.
Basically, you can only alter the state of the object referenced by DataContext. You can't change what object is referenced by it, or by the bound collection.