CheckComboBox ValueMemberPath不起作用

时间:2014-03-05 09:30:35

标签: c# wpf data-binding wpftoolkit

代码隐藏模型(下面代码中的Integers)没有填充选定的项目。这是一个错误还是我错过了什么?

XAML:

<Window x:Class="DevicesRepositoryEditor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:toolKit="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        >
    <StackPanel>
        <toolKit:CheckComboBox x:Name="Ccb"
            Delimiter=","
            VerticalAlignment="Center"
            ItemsSource="{Binding Pool}"
            DisplayMemberPath="Appearance"
            ValueMemberPath="Numeric"
            SelectedItemsOverride="{Binding Integers}" />
        <!-- ...
            some more irrelevant stuff
        ... -->
    </StackPanel>
</Window>

代码:

public class Composite{
    public Composite(string str, int num){ Appearance = str; Numeric = num; }
    public string Appearance { get; set; }
    public int Numeric { get; set; }
}

public partial class MainWindow : Window {
    public ObservableCollection<Composite> Pool { get; set; }
    public ObservableCollection<int> Integers { get; set; }

    public MainWindow(){
        Pool = new ObservableCollection<Composite>{
            new Composite("one", 1),
            new Composite("two", 2),
            new Composite("three", 3),
            new Composite("four", 4),
        };
        Integers = new ObservableCollection<int>();
        InitializeComponent();
    }

    /* ...
        some more irrelevant stuff
    ... */
}

3 个答案:

答案 0 :(得分:0)

尝试按如下方式定义可观察集合:

public ObservableCollection<Composite> Pool { get { return _pool; } }
private ObservableCollection<Composite> _pool = new ObservableCollection<Composite>(); 

并逐一添加:

public MainWindow(){
    Pool.Add(new Composite("one", 1));
    Pool.Add(new Composite("two", 2));
}

和/或

public class Composite : DependencyObject {
    public Composite(string str, int num){ Appearance = str; Numeric = num; }
    public string Appearance
    {
        get { return (string)GetValue(AppearanceProperty); }
        set { SetValue(AppearanceProperty, value); }
    }
    public static readonly DependencyProperty AppearanceProperty =
        DependencyProperty.Register("Appearance", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null));

    public int Numeric
    {
        get { return (int)GetValue(NumericProperty); }
        set { SetValue(NumericProperty, value); }
    }
    public static readonly DependencyProperty NumericProperty =
        DependencyProperty.Register("Numeric", typeof(int), typeof(MainWindow), new UIPropertyMetadata(0));
}

答案 1 :(得分:0)

您需要设置DataContext。添加

DataContext = this;

之前InitializeComponents

答案 2 :(得分:0)

您必须使用SelectedValue属性以双向模式绑定整数,请尝试此操作。我想这可能会对你有帮助