不能绑定枚举到组合框wpf mvvm [英] Cant bind enum to combobox wpf mvvm

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

问题描述

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

A have read a lot of method about the ways of binding enum to combobox. So now in .Net 4.5 it should be pretty ease. But my code dont work. Dont really understand why.

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后端



xaml.cs backend

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是枚举。
我所有的都是空的下拉列表

StrategyType is enum. All i have got it is empty dropdown list

推荐答案

您正试图绑定到一个私有变量,而应该将枚举显示为 Property

You are trying to bind to a private variable, instead, your enum should be exposed as a Property.

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

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

Also, Discosultan has already solved another problem for you.

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

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