1

我让我的应用程序处理在一个类中创建的字段,其中每个字段以通常的方式单独生成

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 吗?

4

2 回答 2

1

使用模式=双向

<TextBox Height="23" Name="txtBxInterfaceName" Width="250" 
     Text="{Binding Path=InterfaceName.Field, Mode=TwoWay}" />

当您的 ETLBaseField 正在实现 INotifyPropertyChanged 时,DataBinding 应该可以正常工作。如果它不起作用,请尝试显式设置 TextBox 的 DataContext

<TextBox DataContext="{Binding InterfaceName}" Height="23" Name="txtBxInterfaceName" Width="250" 
 Text="{Binding Field}" />

你养OnPropertyChanged错了!

不是这个

OnPropertyChanged(strFieldName);

而是这样做

OnPropertyChanged("Field");
于 2013-05-21T03:02:21.570 回答
0

我认为您需要使用 Getter 将 InterfaceName 转换为属性。我认为它不会像您的示例那样与 Field in 一起使用。所以,试试这个:

private ETLBaseField _interfaceName;
public ETLBaseField InterfaceName
{
  get{return _interfaceName??(_interfaceName = new ETLBaseField("InterfaceName"));
  set{_interfaceName = value;}
}

如果您不想使用,则可能不需要 Setter。

于 2013-05-21T02:50:41.113 回答