Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Enums to DataGrid ComboBox View

Tags:

c#

.net

wpf

I am working with a WPF application, and I am working on a DataGrid that incorporates the use of dynamic content that must react to events,etc.

I have the following ViewModel for the View that contains the DataGrid

public class HiddenFieldPanelViewModel
{
    public List<HiddenFieldComponent> HiddenFieldList { get; set; }
    public HiddenFieldComponent Component { get; set; }
    public bool IsVisible { get; set; }
    public enum FieldTypes{Constant,Variable}
    public HiddenFieldPanelViewModel()
    {
        HiddenFieldList = new List<HiddenFieldComponent>();
        IsVisible = false;
    }
}

The only property on this model that applies to this example is the following enum property

public enum FieldTypes {Constant,Variable}

What I need to do when the DataGrid is populated is to bind the enum types to the dropdown that is in the DataGrid cell, here is an example of one of the DataGrid collection items after it would have been added enter image description here

So for example, in the picture above, I would like it to have both of the Enum Values from the FieldTypes enum.

In my XAML, I have specified the following:

<DataGridTemplateColumn Header="Field Type" CanUserResize="False">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Height="20" SelectedIndex="0" ItemsSource="{Binding Path=FieldTypes}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Path=Value}"></Label>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

The other columns are binding the data correctly, but this one is not.

I am not sure if there is a better way to do it or not. I have also written a EnumConverter from the IValueConverter to handle the string-enum-string conversions if that is ever needed.

Thanks

like image 526
TheJediCowboy Avatar asked Mar 25 '11 19:03

TheJediCowboy


1 Answers

Pull the enum out of your ViewModel

public enum FieldTypes
{
  Constant,
  Variable,
}

// Don't forget to set up your INotifyPropertyChanged on your properties
// if they are being used for binding
public class HiddenFieldPanelViewModel
{
    public List<HiddenFieldComponent> HiddenFieldList { get; set; }
    public HiddenFieldComponent Component { get; set; }
    public bool IsVisible { get; set; }

    // removed:
    // public enum FieldTypes{Constant,Variable}

    // will likely want to set up a property such as:
    // public enum FieldTypes {get; set;}

    public HiddenFieldPanelViewModel()
    {
        HiddenFieldList = new List<HiddenFieldComponent>();
        IsVisible = false;
    }
}

These would be the namespaces you import into your xaml:

xmlns:local="clr-namespace:NamespaceToYourEnum" 
xmlns:System="clr-namespace:System;assembly=mscorlib"

And then you can set up an ObjectDataProvider to bind the Combobox. Some sample XAML:

<Window.Resources>    
    <ObjectDataProvider x:Key="EnumDataProvider" 
                        MethodName="GetValues" 
                        ObjectType="{x:Type System:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:FieldTypes"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

<!-- FieldTypesEnumProperty would be in your ViewModel -->
<ComboBox Height="25"
        SelectedItem="{Binding Path=FieldTypesEnumProperty}"
        ItemsSource="{Binding Source={StaticResource EnumDataProvider}}" />
like image 125
Metro Smurf Avatar answered Sep 22 '22 03:09

Metro Smurf