简体   繁体   中英

Binding Enums to DataGrid ComboBox View

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 在此输入图像描述

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

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}}" />

Take a look at something like :

<DataTemplate>
  <ComboBox SelectedValue="{Binding Path=EstimateStatusValueId}"
            ItemsSource="{Binding Path=DataContext.EstimateStatusValueList, 
              RelativeSource={RelativeSource FindAncestor, 
                AncestorType={x:Type UserControl}}}"
            DisplayMemberPath="Description"
            SelectedValuePath="EstimateStatusValueId" />
</DataTemplate>

For anyone still looking for answer:
In .net 4.5 you just need property with type of your enum. DataGrid automatically creates combobox for you.
Something like this:

public enum FieldTypes
{
  Constant,
  Variable,
}

public class HiddenFieldPanelViewModel
{
  public FieldTypes Types { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM