WPF:无法将 DataGrid 绑定到枚举列表

发布于 2024-10-25 12:05:43 字数 2194 浏览 0 评论 0原文

我在绑定到枚举列表时遇到意外问题。绑定悄然失败,我无法解释原因。

此代码应该做的是创建一个 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>

只是一些注释:

  1. 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:

  1. local:Enumeration is a markup extension which simply provides the descriptions of the enum elements as a list.

Thanks in advance.

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

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

发布评论

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

评论(1

柒夜笙歌凉 2024-11-01 12:05:43

绑定集合的项目将复制到单元格的 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.

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