質問

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

役に立ちましたか?

解決

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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top