C# 选中组合框值MemberPath不';行不通
代码隐藏模型(C# 选中组合框值MemberPath不';行不通,c#,wpf,data-binding,wpftoolkit,C#,Wpf,Data Binding,Wpftoolkit,代码隐藏模型(Integers,在下面的代码中)没有用所选项目填充。是虫子还是我错过了什么 XAML: 代码: 公共类复合{ 公共组合(字符串str,int num){Appearance=str;Numeric=num;} 公共字符串外观{get;set;} 公共整数数值{get;set;} } 公共部分类主窗口:窗口{ 公共ObservableCollection池{get;set;} 公共可观测集合整数{get;set;} 公共主窗口(){ 池=新的可观察集合{ 新复合材料(“一”,1
Integers
,在下面的代码中)没有用所选项目填充。是虫子还是我错过了什么
XAML:
代码:
公共类复合{
公共组合(字符串str,int num){Appearance=str;Numeric=num;}
公共字符串外观{get;set;}
公共整数数值{get;set;}
}
公共部分类主窗口:窗口{
公共ObservableCollection池{get;set;}
公共可观测集合整数{get;set;}
公共主窗口(){
池=新的可观察集合{
新复合材料(“一”,1),
新的复合材料(“2”,2),
新复合材料(“三”,3),
新复合材料(“四”,4),
};
整数=新的ObservableCollection();
初始化组件();
}
/* ...
一些更不相关的东西
... */
}
尝试按如下方式定义可观察集合:
public ObservableCollection<Composite> Pool { get { return _pool; } }
private ObservableCollection<Composite> _pool = new ObservableCollection<Composite>();
和/或
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;
在初始化组件之前,必须使用SelectedValue属性以双向模式绑定整数。请尝试此操作。我想这可能对你有帮助不起作用。甚至尝试将
ValueMemberPath=“Numeric”
更改为ValueMemberPath=“{Binding Numeric}”
。但仍然不起作用。但我在XAML中有DataContext=“{Binding RelativeSource={RelativeSource Self}}”
。不管怎样,试过了,不起作用:-(
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 = this;