绑定自定义属性类 WPF
我有一个名为“RFPGegevens”的自定义 WPF 属性类
public class RFPGegevens
{
private string _klant;
public String Klant
{
get { return _klant; }
set { _klant = value; }
}
}
在我的 ViewModel 中,我有一个属性 RFPGegevens
private RFPGegevens _rfpGegevens;
public RFPGegevens RfpGegevens
{
get
{
if (_rfpGegevens == null)
_rfpGegevens = new RFPGegevens();
return _rfpGegevens;
}
set
{
_rfpGegevens = value;
base.RaisePropertyChangedEvent("RfpGegevens");
}
}
该属性已正确填充,如果我调试,结果是:
在我的视图中,我将属性“RFPGegevens”绑定到我的网格的数据上下文
<Grid DataContext="{Binding RfpGegevens}">
,并且如果我调试,“Klant”属性字段仍然被填充。
但是当我在视图中绑定此属性时,我的文本框仍然为空。
<TextBox Text="{Binding Klant, Mode=TwoWay}"/>
我也尝试过这个,但似乎没有任何作用:
<TextBox Text="{Binding RFPGegevens.Klant, Mode=TwoWay}"/>
不知道我做错了什么。
提前致谢 ;)
I got a custom WPF property class named "RFPGegevens"
public class RFPGegevens
{
private string _klant;
public String Klant
{
get { return _klant; }
set { _klant = value; }
}
}
In my ViewModel I got a property RFPGegevens
private RFPGegevens _rfpGegevens;
public RFPGegevens RfpGegevens
{
get
{
if (_rfpGegevens == null)
_rfpGegevens = new RFPGegevens();
return _rfpGegevens;
}
set
{
_rfpGegevens = value;
base.RaisePropertyChangedEvent("RfpGegevens");
}
}
This property is filled correctly, if I debug this is the result:
In my View I'm binding the property "RFPGegevens" to the datacontext of my Grid
<Grid DataContext="{Binding RfpGegevens}">
and still if I debug the "Klant" property field is filled.
But as I bind this property in the View my textbox is still empty.
<TextBox Text="{Binding Klant, Mode=TwoWay}"/>
I also tried this but nothings seems to work:
<TextBox Text="{Binding RFPGegevens.Klant, Mode=TwoWay}"/>
Don't know what I'm doing wrong.
Thanks in advance ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试做两件事
try with two things
<TextBox Text="{Binding Klant, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
您还需要为自定义类实现 INotifyPropertyChanged 接口,如下所示:
并从属性的 set 访问器引发 propertychanged 事件。
You need to implement the INotifyPropertyChanged interface for your custom class also as below:
and raise the propertychanged events from the set accessor of your properties.
您忘记添加 INotifyPropertyChanged 的继承
You forget to add inheritance from INotifyPropertyChanged