开发者

WPF: Cannot bind DataGrid to enum list

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="Equip开发者_JS百科ment 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜