开发者

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 app开发者_高级运维lies 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

Binding Enums to DataGrid ComboBox View

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜