无法将enum绑定到comboboxwpf mvvm
A已经阅读了很多关于将枚举绑定到combobox的方法。 所以现在在.Net 4.5中它应该非常容易。 但我的代码不起作用。 真的不明白为什么。
XAML:
xaml.cs后端
namespace SmartTrader.Windows { /// /// Interaction logic for SyncOfflineDataWindow.xaml /// 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 StrategyTypes = Enum.GetValues(typeof (StrategyType)).Cast(); public ObservablePosition(IPosition position) { Strategy = position.Strategy; } private StrategyType _strategyType = StrategyType.None; public StrategyType Strategy { get { return _strategyType; } set { _strategyType = value; OnPropertyChanged(); } } } }
StrategyType是枚举。 我所有的都是空的下拉列表
您正在尝试绑定到私有变量,而您的枚举应该作为Property
公开。
public IEnumerable StrategyTypes { get { return Enum.GetValues(typeof(StrategyType)).Cast(); } }
此外,Discosultan已经为您解决了另一个问题。
在wpf XAML中将任何枚举数据绑定到combobox的最简单方法:在窗口或用户控制资源中添加数据提供程序
的xmlns:亲= “CLR-名称空间:TestProject”>