我可以将 WPF 控件绑定到字段的属性吗? [英] Can I bind a WPF control to a field's property?
本文介绍了我可以将 WPF 控件绑定到字段的属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因为我需要在类之间拆分一些功能,所以我遇到了以下情况
Because I needed to split some functionality between classes, I've arrived at the following situation
xml 代码
<CheckBox IsChecked="{Binding MyObjectField.MyBoolean}" />
查看模型
...
public MyInternalObject MyObjectField;
...
MyObject 类
public class MyInternalObject {
...
public bool MyBoolean { get; set; }
...
}
除非我在 View Model 类中复制 MyBoolean 属性,否则它不起作用.
It does not work unless I replicate the MyBoolean property in the View Model class.
public bool MyBoolean
{
get { return MyInternalObject.MyBoolean; }
set { MyInternalObject.MyBoolean=value; }
}
有人有想法吗?
推荐答案
不,你不能.因为绑定系统使用反射来查找
No you cant . Because binding system uses Reflection to find the
DataContext 中的属性(即您的虚拟机)
Property in DataContext(i.e your VM)
它不寻找 fields .我希望这会有所帮助.
It does not look for fields . I hope this will help.
这篇关于我可以将 WPF 控件绑定到字段的属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文