在C#WPF中实现INotifyPropertyChanged

时间:2014-06-06 18:17:26

标签: c# wpf inotifypropertychanged

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

[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));
    }
}

3 个答案:

答案 0 :(得分:2)

另一种转换方法是使用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>

无需其他更改。

答案 1 :(得分:1)

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

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

可能的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;
}            

}

答案 2 :(得分:1)

每当您需要将某些内容转换为其他内容时,您都可以使用转换器。转换器只是一个实现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}"/>

还有一个问题。改变&#34; DisplayFormat&#34;将导致NotifyPropertyChanged触发,这将更新转换器内的值。但是,UI不运行转换功能,因为它不会认为任何内容已发生变化。因此,你需要&#34;假装&#34;通过调用 NotifyPropertyChanged来改变该值:

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

现在,用户界面将执行一个新的&#34; get&#34;转换并显示你想要的东西!

相关问题