DataGridComboBoxColumn binding to List<Enum>

喜你入骨 提交于 2019-12-13 00:28:58

问题


I want to bind a list of enum values to a 'DataGridComboBoxColumn'. I've tried a lot, but nothing really works.

Here is what I have:

viewmodel-class:

public class ViewModel
    {

        public ViewModel()
        {
            TestCollection= new ObservableCollection<MyEnum>();
            AnyClasses = new ObservableCollection<AnyClass>();

            //... fill AnyClasses with stuff...

            TestCollection.Add(MyEnum.Value1);
            TestCollection.Add(MyEnum.Value2);
            TestCollection.Add(MyEnum.Value3);
            TestCollection.Add(MyEnum.Value4);
            TestCollection.Add(MyEnum.Value5);

        }

        public ObservableCollection<MyEnum> TestCollection { get; set; }
       public ObservableCollection<AnyClass> AnyClasses { get; private set; }

}

my enum:

public enum MyEnum
    {
        Value1,
        Value2,
        Value3,
        Value4,
        Value5
    }

Codebehind:

 public partial class WPFWindow
    {
        private ViewModel Vm { get; set; }

        public WPFWindow() 
        { 
            InitializeComponent(); 
            Vm = new ViewModel(); 
            DataContext = Vm; 
        }
...
    }

and finally the XAML:

<DataGrid AutoGenerateColumns="False" Height="289" x:Name="dataGridAnything" ItemsSource="{Binding AnyClasses}" >
  <DataGrid.Columns>
    <DataGridComboBoxColumn Width="200" Header="Optionen" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}, Path=DataContext.TestCollection}" SelectedValuePath="Value"/>
  </DataGrid.Columns>
</DataGrid>

If I start the project, nothing is displayed in the DataGridComboBoxColumn. What did I do wrong? Thx for you help.

Edit: you can download the project (built with VS 2013) here: Download


回答1:


Use ObjectDataProvider as mentioned in this example. It's the preferred way of binding enum to combo boxes since you don't have to manually fill enum collection in your code.

Declare ObjectDataProvider in your resources:

<ObjectDataProvider x:Key="myEnum" MethodName="GetValues" ObjectType="{x:Type core:Enum}">
    <ObjectDataProvider.MethodParameters>
        <x:Type Type="{x:Type l:MyEnum}"/>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

(You have to declare your local namespace l and core namespace: xmlns:core="clr-namespace:System;assembly=mscorlib")

then bind DataGridComboBoxColumn to it:

<DataGridComboBoxColumn Width="200" Header="Optionen" ItemsSource="{Binding Source={StaticResource myEnum}}" SelectedValuePath="Value"/>

EDIT

Since you have to modify your enum collection in runtime, take a look at this question

In short, it's a known issue of DataGridComboBoxColumn. You have to alter its element style:

<DataGridComboBoxColumn Width="200" Header="Optionen">
    <DataGridComboBoxColumn.ElementStyle>
        <Style>
            <Setter Property="ComboBox.ItemsSource" 
                                    Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, 
                                Path=DataContext.TestCollection}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style>
            <Setter Property="ComboBox.ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, 
                                Path=DataContext.TestCollection}" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>


来源:https://stackoverflow.com/questions/23535159/datagridcomboboxcolumn-binding-to-listenum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!