如何将Enum从ViewModel绑定到ComboBox - twoway

时间:2016-08-07 16:29:05

标签: c# wpf data-binding visual-studio-2015

[已解决] :我在下面的答案中提供了一个工作示例。

<小时/> [SO] [1]

上的相关主题

我在这里阅读了很多线程,如何将枚举绑定到组合框。 我用另一种方法使用MarkupExtension让它一起工作! 但是,我没有双重约束。

我想将它绑定到我的ViewModel的Enum属性。

当我尝试这种方法时:

 <Window.Resources>
    <ObjectDataProvider MethodName="GetValues"
                        ObjectType="{x:Type sys:Enum}"
                        x:Key="DetailScopeDataProvider">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:DetailScope" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

这里的另一个问题是,VS intellisense不会给我这个:

  

SYS:枚举

我也不知道设置组合框的ItemSource和SelectedValue属性。

1 个答案:

答案 0 :(得分:0)

好的,让它运转起来。

ObjectDataProvider具有ObjectType属性。因为我想使用我的viewmodel的Enum属性作为组合框的数据源。 在这种情况下,对象类型是:

  

System.Enum

位于mscorlib程序集的System命名空间中。 因此,我们必须在XAML中添加名称空间:

  

的xmlns:SYS = “CLR-命名空间:系统;装配= mscorlib程序”

这里我将发布整个代码供参考:

<强> Test.xaml(窗口):

<Window x:Class="WpfTrash.Test"
        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:WpfTrash"
   -->> xmlns:sys="clr-namespace:System;assembly=mscorlib" <<--
        mc:Ignorable="d"
        Title="Test" Height="300" Width="300">
    <Window.Resources>
        <local:MyEnumToStringConverter x:Key="MyEnumConverter" />
        <ObjectDataProvider x:Key="odp" MethodName="GetNames" ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type  TypeName="local:TheEnum"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

    </Window.Resources>
    <StackPanel>
        <TextBox x:Name="txtTheInt" Text="{Binding Path=TheInt}"/>
        <TextBox x:Name="txtTheString" Text="{Binding Path=TheString}"/>
        <!--<ComboBox x:Name="cboTheEnum" DataContext="{StaticResource SortedEnumView}" ItemsSource="{Binding}"/>-->
        <ComboBox x:Name="cboTheEnum" ItemsSource="{Binding Source={StaticResource odp}}" SelectedValue="{Binding Path=TheEnum, Converter={StaticResource MyEnumConverter}}"/>
        <Button x:Name="button" Content="Button" Click="button_Click"/>
    </StackPanel>
</Window>

<强> Test.xaml.cs

namespace WpfTrash
{
    /// <summary>
    /// Interaction logic for Test.xaml
    /// </summary>
    public partial class Test : Window
    {
        MyClass vm = new MyClass();
        public Test()
        {
            InitializeComponent();
            this.DataContext = vm;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            var selecteditem = (TheEnum)Enum.Parse(typeof(TheEnum), cboTheEnum.SelectedItem.ToString(), true);
        }
    }
}

视图模型:

public class MyClass : INotifyPropertyChanged
{
    public int TheInt { get; set; }
    public string TheString { get; set; }

    TheEnum theEnum;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public TheEnum TheEnum
    {
        get
        {
            return theEnum;
        }

        set
        {
            theEnum = value;
            NotifyPropertyChanged("TheEnum");
        }
    }
}

枚举定义:(不属于类定义)

public enum TheEnum
{
    A, B, C, D
}

转化器:

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 (TheEnum)Enum.Parse(typeof(TheEnum), value.ToString(), true);
    }
}
相关问题