将组合框的SelectedValue绑定到WPF中的枚举 [英] Binding SelectedValue of ComboBox to enum in WPF

查看:185
本文介绍了将组合框的SelectedValue绑定到WPF中的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 ListView 中显示产品列表,其中一列是我想要的 ComboBox 绑定这是我的枚举:

I want to show list of products in ListView where one of the columns is a ComboBox that I want to bind. This is my enum:

public enum SelectionMode { One, Two }

和产品类:

public class Product
{
    public SelectionMode Mode { get; set; }

    public string Name { get; set; }
}

ViewModel 我有一个 ObservableCollection 产品

    private ObservableCollection<Product> _productList;
    public ObservableCollection<Product> ProductList
    {
        get
        {
            return _productList;
        }
        set
        {
            _productList = value;
        }
    }

    public MainViewModel()
    {
        ProductList = new ObservableCollection<Product>
                          {
                              new Product {Mode = SelectionMode.One, Name = "One"},
                              new Product {Mode = SelectionMode.One, Name = "One"},
                              new Product {Mode = SelectionMode.Two, Name = "Two"}
                          };
    }

最后我有一个 Grid ListView 绑定到我的 ProductList

And finally I have a Grid with a ListView that binds to my ProductList:

<Window.Resources>
    <ObjectDataProvider x:Key="AlignmentValues" 
                    MethodName="GetNames" ObjectType="{x:Type System:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="ViewModel:SelectionMode" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

<Grid>
    <ListView Height="120" HorizontalAlignment="Left" 
                  VerticalAlignment="Top"
                  SelectionMode="Multiple" 
                  ItemsSource="{Binding ProductList}" >
        <ListView.View>
            <GridView>
                <GridViewColumn Width="120" Header="Product Name" DisplayMemberBinding="{Binding Path=Name}" />
                <GridViewColumn Header="Selection Mode">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Source={StaticResource AlignmentValues}}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

我的问题是将 SelectedValue ComboBox 绑定到 SelectionMode 我的产品的财产类?

My question is; what is the way to bind SelectedValue of ComboBox to SelectionMode property of my Product class?

更新

嗯我在本主题。所以我必须添加转换器类:

Well. I found an answer in this topic. So I have to add converter class:

public class MyEnumToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (SelectionMode)Enum.Parse(typeof(SelectionMode), value.ToString(), true);
    }
}

并将其添加到窗口资源:

And add it to window resources:

<Window.Resources>
    <ObjectDataProvider x:Key="AlignmentValues" 
                    MethodName="GetNames" ObjectType="{x:Type System:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="ViewModel:SelectionMode" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <Converters:MyEnumToStringConverter x:Key="MyEnumConverter"/>
</Window.Resources>

最后编辑 ComboBox 数据模板: / p>

And finally edit ComboBox data template:

<ComboBox ItemsSource="{Binding Source={StaticResource AlignmentValues}}" 
                                      SelectedValue="{Binding Path=Mode, Converter={StaticResource MyEnumConverter}}"/>


希望对其他人有用:)

That's all. Hope it will be useful for someone else :)

推荐答案

如果您准备好更改ItemsSource的绑定的ComboBox然后,只需 SelectedValue ={Binding Mode,UpdateSourceTrigger = PropertyChanged,Mode = TwoWay}将工作。

If you are ready to change the binding of the ItemsSource of the ComboBox then, simply SelectedValue="{Binding Mode,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" will work.

在这种情况下,您必须像这样绑定ItemsSource: ItemsSource ={Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type ViewClass}},Path = ModeList};其中,ModeList是 SelectionMode 类型的列表的简单公共属性,包含应在ComboBox下拉列表中显示的枚举,ViewClass是此属性(ModeList)为一个可用的确保命名空间的引用被添加到xaml中。

In this case you have to bind the ItemsSource like this: ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ViewClass}}, Path=ModeList}"; where, the ModeList is a simple public property of list of SelectionMode type, contains the enums which should be displayed in ComboBox dropdown and ViewClass is the class where this property (ModeList) is a available; make sure the reference of the namespace is added in the xaml.

否则你必须使用一个转换器,该转换器应该将该字符串转换为枚举类型。

Otherwise you have to use a converter, which should convertback the string to the enum type.

这篇关于将组合框的SelectedValue绑定到WPF中的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆