[英]binding a custom property class WPF
I got a custom WPF property class named "RFPGegevens" 我有一个名为“RFPGegevens”的自定义WPF属性类
public class RFPGegevens
{
private string _klant;
public String Klant
{
get { return _klant; }
set { _klant = value; }
}
}
In my ViewModel I got a property RFPGegevens 在我的ViewModel中,我得到了一个属性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 在我的视图中,我将属性“RFPGegevens”绑定到我的Grid的datacontext
<Grid DataContext="{Binding RfpGegevens}">
and still if I debug the "Klant" property field is filled. 如果我调试“Klant”属性字段仍然填充。
But as I bind this property in the View my textbox is still empty. 但是当我在View中绑定这个属性时,我的文本框仍然是空的。
<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 ;) 提前致谢 ;)
try with two things 尝试两件事
<TextBox Text="{Binding Klant, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
public class RFPGegevens
{
private string _klant;
public String Klant
{
get { return _klant; }
set {
_klant = value;
//Raise the property changed event here
}
}
}
You need to implement the INotifyPropertyChanged interface for your custom class also as below: 您需要为自定义类实现INotifyPropertyChanged接口,如下所示:
public class RFPGegevens : INotifyPropertyChanged
and raise the propertychanged events from the set accessor of your properties. 并从属性的set访问器中引发propertychanged事件。
您忘记从INotifyPropertyChanged添加继承
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.