我让我的应用程序处理在一个类中创建的字段,其中每个字段以通常的方式单独生成
public class RequestProfileObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string strPassword;
public string Pwd
{
get { return strPassword; }
set
{
strPassword = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("Pwd");
}
}
//Plus other code to load the data into the field and the OnPropertyChanged
//method
}
然后我发现我需要能够跟踪更改,并为此提出了一种机制以及我需要的一些其他功能。不用说很多重复的代码使我产生了为这些对象创建一个类的好主意。
public class ETLBaseField : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string strFieldPrime;
private string strField;
private string strFieldName;
public ETLBaseField() { }
public ETLBaseField(string strInputField)
{
strFieldName = strInputField;
}
public string Field
{
get { return strField; }
set
{
if (strField != value)
{
strField = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged(strFieldName);
}
}
}
public void Load(string strValue)
{
strFieldPrime = strValue;
strField = strFieldPrime;
}
public void Empty()
{
Load("");
}
public bool isDirty()
{
return strFieldPrime != Field;
}
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
这个类被我的主类使用
public class RequestProfileObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ETLBaseField RequestName = new ETLBaseField("RequestName");
public ETLBaseField InterfaceName = new ETLBaseField("InterfaceName");
//Plus code that loads the ETLBaseField things and the OnPropertyChanged method
}
我的 xaml 代码正在运行,我对字段的绑定只是笨拙的多莉。现在有了我的新发明,我不知道如何将 RequestName.Field 属性绑定到 xaml 文本框。以下
<TextBox Height="23" Name="txtBxInterfaceName" Width="250"
Text="{Binding Path=InterfaceName.Field}" />
不起作用。在创建 ETLBaseField 类时,我是不是太聪明了?我应该从 RequestProfileObject 中删除 INPC 吗?