簡體   English   中英

CheckComboBox ValueMemberPath不起作用

[英]CheckComboBox ValueMemberPath doesn't work

代碼隱藏模型(下面的代碼中為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
    ... */
}

嘗試如下定義可觀察的集合:

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

您需要設置DataContext

DataContext = this;

InitializeComponents之前

您必須使用SelectedValue屬性以兩種方式綁定整數,請嘗試此操作。 我認為這可能對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM