【问题标题】:Cant bind enum to combobox wpf mvvm无法将枚举绑定到组合框 wpf mvvm
【发布时间】:2015-08-20 08:50:20
【问题描述】:

A 已经阅读了很多关于将枚举绑定到组合框的方法的方法。所以现在在 .Net 4.5 中应该很容易。但我的代码不起作用。 不太明白为什么。

xaml:

<Window x:Class="SmartTrader.Windows.SyncOfflineDataWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SyncOfflineDataWindow" Height="300" Width="300">
<Grid>
    <StackPanel>
        <ComboBox ItemsSource="{Binding StrategyTypes}" SelectedItem="{Binding StrategyType}" />
        <Button Width="150" Margin="5" Padding="5" Click="Button_Click">Save</Button>
    </StackPanel>
</Grid>

xaml.cs 后端

namespace SmartTrader.Windows
{
    /// <summary>
    /// Interaction logic for SyncOfflineDataWindow.xaml
    /// </summary>
    public partial class SyncOfflineDataWindow : Window
    {
        public SyncOfflineDataWindow(IPosition position, ContractType type)
        {
            DataContext = new ObservablePosition(position);
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}

查看模型:

namespace SmartTrader.Entity
{
    public class ObservablePosition : NotifyPropertyChanged, IPosition
    {
        public IEnumerable<StrategyType> StrategyTypes =
            Enum.GetValues(typeof (StrategyType)).Cast<StrategyType>();

        public ObservablePosition(IPosition position)
        {
           Strategy = position.Strategy;
        }


        private StrategyType _strategyType = StrategyType.None;
        public StrategyType Strategy
        {
            get { return _strategyType; }
            set
            {
                _strategyType = value;
                OnPropertyChanged();
            }
        }
    }
}

StrategyType 是枚举。 我得到的只是空的下拉列表

【问题讨论】:

  • {Binding StrategyType} - 应该是{Binding Strategy}吗?
  • 他所说的 :) ...您绑定到了错误的属性(好吧,您确实绑定到了一个私有字段,这是您无法做到的)。我确定您在您忽略的输出控制台上遇到了某种绑定错误:)
  • @Novikov - 这对你来说是双向的吗?对我来说,它填充了组合框,但更改组合框中的选定值不会改变(在你的情况下)Strategy。你有没有为此添加任何额外的东西?
  • 应该是SelectedItem 但这并不能改变我无法绑定的事实......?如果我做对了,组合框将包含来自public IEnumerable&lt;StrategyType&gt; StrategyTypes 的结果 - 这里SelectedItem 的类型是什么?我猜这不是StrategyType 而是别的什么?

标签: c# wpf mvvm combobox enums


【解决方案1】:

您正在尝试绑定到私有变量,相反,您的枚举应公开为 Property

public IEnumerable<StrategyTypes> StrategyTypes
{
    get
    {
        return Enum.GetValues(typeof(StrategyType)).Cast<StrategyType>();
    }
}

另外,Discosultan 已经为您解决了另一个问题。

【讨论】:

    【解决方案2】:

    在 wpf XAML 中将任何枚举数据绑定到组合框的最简单方法: 在窗口或用户控件资源中添加数据提供者

    xmlns:pro="clr-namespace:TestProject">

    <UserControl.Resources>
        <ObjectDataProvider x:Key="getDataFromEnum" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="pro:YourEnumName"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </UserControl.Resources>
    <!--ComboBox xaml:-->
    <ComboBox ItemsSource="{Binding Source={StaticResource getDataFromEnum}}"/>
    

    【讨论】:

      【解决方案3】:

      我在 youtube 上找到了解决方案。检查以下链接:

      如何在 WPF 中将枚举绑定到 ComboBox:https://youtu.be/Bp5LFXjwtQ0

      此解决方案创建一个派生自 MarkupExtension 类的新类,并将该类用作 XAML 代码中的源。

      EnumBindingSourceExtention.cs 文件:

      namespace YourProject.Helper
      {
      public class EnumBindingSourceExtention : MarkupExtension
          {
              public Type EnumType { get; private set; }
      
              public EnumBindingSourceExtention(Type enumType)
              {
                  if (enumType == null || !enumType.IsEnum)
                  {
                      throw new Exception("EnumType is null or not EnumType");
                  }
                  this.EnumType = enumType;
              }
      
              public override object ProvideValue(IServiceProvider serviceProvider)
              {
                  return Enum.GetValues(EnumType);
              }
          }
      }
      

      View.Xaml 文件:

      <Window
          xmlns:helper="clr-namespace:YourProject.Helper"/>
      <ComboBox
          ItemsSource="{Binding Source={helper:EnumBindingSourceExtention {x:Type local:TheEnumClass}}}" />
      
      

      local:TheEnumClass: TheEumClass 应该位于您指定命名空间的位置(在这种情况下,它位于本地)

      【讨论】:

        猜你喜欢
        • 2018-01-12
        • 2016-02-06
        • 2018-11-12
        • 2020-02-07
        • 1970-01-01
        • 1970-01-01
        • 2011-08-04
        • 1970-01-01
        • 2010-09-08
        相关资源
        最近更新 更多