kw1jybo
kw1jybo

Reputation: 152

WPF datagridcomboboxcolumn not binding

I'm trying to get a DataGridComboBoxColumn to bind to an enum, but all I get is a Empty dropdown.

I tried a number of things with DisplayMemberPath and other things, but no avail.

The binding on the SkillName works, but not the SkillLevel

XAML:

<Window x:Class="EveCommon.Skills.EveSkillEditWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Common.Skills"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    mc:Ignorable="d"
    Title="Skills Window" Height="600" Width="600"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <ObjectDataProvider x:Key="SkillEnum" MethodName="GetValues" ObjectInstance="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:SkillLevels"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>


        <DataGrid Grid.Row="1" ItemsSource="{Binding Skills}" AutoGenerateColumns="False" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" IsReadOnly="True" Binding="{Binding SkillName}"/>
            <DataGridComboBoxColumn Header="Skill Level" DisplayMemberPath="SkillLevel"
                                    SelectedValueBinding="{Binding Path=SkillLevel}"
                                    ItemsSource="{Binding Source={StaticResource SkillEnum}}"/>
        </DataGrid.Columns>
    </DataGrid>

Enum:

public enum SkillLevels
{
    Zero = 0,
    I,
    II,
    III,
    IV,
    V
}

Skill class:

public class Skill : ImplementPropertyChanged
{
    private string _skillName;
    private SkillLevels _skillLevel;

    public string SkillName
    {
        get => this._skillName;
        set => this.SetField(ref this._skillName, value);
    }

    public SkillLevels SkillLevel
    {
        get => this._skillLevel;
        set => this.SetField(ref this._skillLevel, value);
    }

}   

And the collection:

    public ObservableCollection<Skill> Skills
    {
        get => this._skills;
        set
        {
            this._skills = value;
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Skills"));
        }
    }

Upvotes: 0

Views: 41

Answers (1)

dhilmathy
dhilmathy

Reputation: 2868

There are two issues in your XAML code. First at your ObjectDataProvider; it should be ObjectType instead of ObjectInstance. Second one is, you should not specify DisplayMemberPath for this as your are displaying the Enum values.

Try this

<ObjectDataProvider x:Key="SkillEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="local:SkillLevels"/>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

Your DataGridComboBoxColumn will be,

<DataGridComboBoxColumn Header="Skill Level"
         SelectedValueBinding="{Binding Path=SkillLevels, UpdateSourceTrigger=PropertyChanged}"
         ItemsSource="{Binding Source={StaticResource SkillEnum}}"/>

Upvotes: 1

Related Questions