在c# WPF中实现INotifyPropertyChanged

本文关键字:实现 INotifyPropertyChanged WPF | 更新日期: 2023-09-27 18:07:18

我是c#和WPF框架的新手,我的问题可能听起来很傻。我正在使用双向绑定功能。所以我实现了INotifyPropertyChanged接口。我有一个名为DisplayFormat的属性,因此每当格式从二进制更改为十六进制时,我的文本应该相应地转换。我的问题是我应该在哪里包括二进制和十进制之间转换的代码/逻辑?

[DefaultValue(displayFormat.Binary)]
public displayFormat DisplayFormat
{
    get { return this.format; }
    set { this.format = value; OnPropertyChanged("DisplayFormat"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

在c# WPF中实现INotifyPropertyChanged

另一种转换方法是使用MultiValueConverter。这并不需要依赖属性或额外的NotifyPropertyChanged。关于MultiValueConverter,需要了解的重要事情是,您将按照在XAML中放置"值"的顺序获得"值"。

转换为:

public class NumberToSpecialStringConverter : IMultiValueConverter
{
    public Convert(...)
    {
       //This is going to the UI, from the Model
       return (value[1] as DisplayFormat).Convert(value[0]); //Or whatever you have
    }
    public ConvertBack(...)
    {
       //This is going to the Model, from the UI
       return (value[1] as DisplayFormat).ConvertBack(value[0]); //Or whatever you have
    }
}
XAML:

<Window.Resources>
   <local:NumberToSpecialStringConverter x:Key="FormatConverter"/>
</Window.Resources>
...
<TextBlock>
   <MultiBinding Converter="{StaticResource FormatConverter}">
      <Binding Path="MyValue"/>
      <Binding Path="DisplayFormat"/>
   </MultiBinding>
</TextBlock>

当您需要将某项内容转换为另一项内容时,您可以使用转换器。转换器只是一个实现IValueConverter的类。

public class NumberToSpecialStringConverter : IValueConverter
{
   ...
}

转换器接受两个输入,值(实际显示或绑定到的任何值)和参数。参数可以是任何您想要的,但不能被绑定。因为你想要一个绑定参数,我们需要从DependencyObject继承,并声明一个DependencyProperty绑定到:

public class NumberToSpecialStringConverter : DependencyObject, IValueConverter
{
    public displayFormat CurrentFormat
    { //Dependency property stuff }
    //Other DP stuff, use the 'propdp' snippet to get it all, or look on MSDN
    public Convert(...)
    {
       //This is going to the UI, from the Model
       return displayFormat.Convert(value); //Or whatever you have
    }
    public ConvertBack(...)
    {
       //This is going to the Model, from the UI
       return displayFormat.ConvertBack(value); //Or whatever you have
    }
}

现在你有了它,你只需要声明和使用它:

<Window.Resources>
   <local:NumberToSpecialStringConverter x:Key="FormatConverter" CurrentFormat="{Binding DisplayFormat}"/>
</Window.Resources>
...
<TextBlock Text="{Binding Path=MyValue, Converter={StaticResource FormatConverter}"/>

还有一个问题。更改"DisplayFormat"将导致NotifyPropertyChanged为fire,这将更新转换器内部的值。但是,UI没有运行转换函数,因为它认为没有任何更改。因此,您需要通过调用它的 NotifyPropertyChanged来"假装"值发生了变化:

[DefaultValue(displayFormat.Binary)]
public displayFormat DisplayFormat
{
    get { return this.format; }
    set
    { 
        this.format = value; 
        OnPropertyChanged("DisplayFormat"); 
        OnPropertyChanged("MyValue");
    }
}

现在UI将执行一个新的"get"和转换,并显示你想要的!

可以做的是有两个属性,都绑定到您的DisplayFormat属性。当应该显示十六进制值时,您可以将二进制内容控件(可能是TextBlock)的可见性设置为false,只显示十六进制值TextBlock,而二进制值则相反。这可以在DisplayFormatNotifyPropertyChanged上使用EventTrigger实现。

另一种方法是在DisplayFormat属性上使用IValueConverter,并具有检查十六进制或二进制并返回正确格式的逻辑。这样,您就可以为每个值呈现合适的格式。

一个可能的IValueConverter实现:

public class BinaryOrHexConverter : IValueConverter    
{        
    public object Convert(object value, Type targetType, object parameter,
                  System.Globalization.CultureInfo culture)        
    {
        int result;
        return int.TryParse(inputString, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result) ? result.ToString() : value.ToString() // Assuming default is binary
    }
public object ConvertBack(object value, Type targetType, object parameter,
                System.Globalization.CultureInfo culture)
{
    return DependencyProperty.UnsetValue;
}            

}