gpt4 book ai didi

c# - CheckComboBox ValueMemberPath 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:11 24 4
gpt4 key购买 nike

代码隐藏模型(下面代码中的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));
}

关于c# - CheckComboBox ValueMemberPath 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22193619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com