【发布时间】: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<StrategyType> StrategyTypes的结果 - 这里SelectedItem的类型是什么?我猜这不是StrategyType而是别的什么?
标签: c# wpf mvvm combobox enums