Associate a DataGridComboBoxColumn with Enum

I have a simple DataGrid that I want the user to add multiple rows. However, I want one of the columns to be a ComboBox with its values ​​taken from the enumeration .

What is the easiest way to do this in my XAML?

I tried following, but I get the error "Two-way binding requires Path or XPath"

<Window.Resources>
    <ObjectDataProvider x:Key="myEnumData"
                MethodName="GetValues" 
                ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:MyEnum" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

...

   <DataGrid.Columns>
        <DataGridComboBoxColumn Header="MyHeader" DisplayMemberPath="EnumValue" 
            SelectedItemBinding="{Binding Source={StaticResource myEnumData}}">
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
+5
source share
2 answers

You are trying to bind a selected item when (presumably) you want to bind a list of available items. Change the binding to this:

<DataGridComboBoxColumn Header="MyHeader"
        ItemsSource="{Binding Source={StaticResource myEnumData}, Mode=OneWay}">
</DataGridComboBoxColumn>
+9
source

Xaml

xmlns:ext="clr-namespace:Project.Core.Tools;assembly=Project.Core"

<DataGridComboBoxColumn ItemsSource="{x:Static ext:Extensions.GetEnumTypes}" SelectedItemBinding="{Binding EnumType}" />

Static class

public static IEnumerable<EnumType> GetEnumTypes => Enum.GetValues(typeof(EnumType)).Cast<EnumType>();
+4
source

All Articles