简体   繁体   中英

CheckComboBox ValueMemberPath doesn't work

The code-behind model ( Integers in the code below) doesn't get filled with selected items. Is it a bug or do I miss something?

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>

Code:

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
    ... */
}

Try defining the observable collection as follows:

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

and add to it one by one:

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

And/Or

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));
}

You need to set the DataContext . Add

DataContext = this;

before InitializeComponents

You have to use SelectedValue property to bind the Integers with two way mode please try this. I think this may help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM