简体   繁体   English

INotifyPropertyChanged不会更新Value Converter

[英]INotifyPropertyChanged does not update Value Converter

I have some properties which implement the INotifyPropertyChanged interface. 我有一些实现INotifyPropertyChanged接口的属性。 It works fine. 它工作正常。 But in my code I also use some value converters (if value < 3 - make grid red, if value >3 and value < 10 - make grid blue, etc.). 但在我的代码中我也使用了一些值转换器(如果值<3 - 使网格为红色,如果值> 3且值<10 - 使网格为蓝色等)。

The problem is how to refresh value converter after PropertyChanged was raised? 问题是如何在引发PropertyChanged后刷新值转换器? Is there simple code behind solution? 解决方案背后有简单的代码吗? Thanks all and sorry for my bad English! 谢谢大家,抱歉我的英语不好!

Here some code: 这里有一些代码:

public class NotifyColors : INotifyPropertyChanged
{
    private Color _TodayColor;
    public Color TodayColor
    {
        get
        {
            return _TodayColor;
        }
        set
        {
            if (_TodayColor != value)
            {
                _TodayColor = value;
                OnPropertyChanged("TodayColor");
            }
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
            // it raised correctly when I change color with color picker control
        }
    }
}

// here is value converter
[ValueConversion(typeof(object), typeof(Brush))]
public class PositionToBackgroundConverter : IValueConverter
{
    ModulePreferences ModulePrefs;
    public PositionToBackgroundConverter(ModulePreferences ModulePrefs)
    {
        this.ModulePrefs = ModulePrefs;
    }

    #region IValueConverter Member

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (ModulePrefs.UseColoringByPosition)
        {
            try
            {
                if (value != null)
                {
                    short value_short = (short)value;
                    if (value_short <= 3)
                        return (Brush)new SolidColorBrush(ModulePrefs.NotifyColorsObj._TodayColor); // here is changing property
                    else
                        return (Brush)new SolidColorBrush(ModulePrefs.NotifyColorsObj.T100PlusColor);
                }
                else
                    return Brushes.Transparent;
            }
            catch
            {
                return Brushes.Transparent;
            }
        }
        else
            return Brushes.Transparent;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }

    #endregion
}

And here I apply my value converter to the grid: 在这里,我将我的值转换器应用于网格:

// assign backgroundconverter
var grid = new FrameworkElementFactory(typeof(Grid));
bin = new Binding();
bin.Path = new PropertyPath(string.Format("DataItem.{0}", LastPositionColumnName));
bin.Converter = new PositionToBackgroundConverter(ProjectViewObj.ModulePrefs);
grid.SetValue(Grid.BackgroundProperty, bin);

If you have done it "correctly" the PropertyChanged event will cause an update of the bindings which bind to that property, when this occurs any converters that are used in the same binding will reconvert the values. 如果“正确”完成了它, PropertyChanged事件将导致绑定到该属性的绑定的更新,当发生这种情况时, 在同一绑定中使用的任何转换器将重新转换值。 So normally the conversions happen on their own. 通常情况下,转换是自己发生的。 If this is not the case you are probably using the converters "inappropriately", please post some code because without it it's quite impossible to tell what exactly you are doing wrong. 如果不是这种情况,你可能会“不恰当地”使用转换器,请发布一些代码,因为如果没有它,就很难说出你究竟做错了什么。

Edit: You use grid.SetValue(Grid.BackgroundProperty, bin) , you should use grid.SetBinding(Grid.BackgroundProperty, bin) instead since it is a binding. 编辑:你使用grid.SetValue(Grid.BackgroundProperty, bin) ,你应该使用grid.SetBinding(Grid.BackgroundProperty, bin)因为它是一个绑定。

Edit2: This really has nothing to do with converters. Edit2:这与转换器无关。
In your sample code you bind to IntValue , then you change TodayColor and expect the binding to be updated, not gonna happen . 在您的示例代码中绑定到IntValue ,然后您更改TodayColor并期望更新绑定, 而不会发生 If you want the binding to react to both properties you have to either use a MultiBinding or raise the respective events since your properties are interdependent. 如果希望绑定对两个属性做出反应,则必须使用MultiBinding或引发相应的事件,因为属性是相互依赖的。

ie

    private Color _TodayColor;
    public short _IntValue;

    public short IntValue
    {
        get { return _IntValue; }
        set
        {
            if (_IntValue != value)
            {
                _IntValue = value;
                OnPropertyChanged("IntValue");
                OnPropertyChanged("TodayColor");
            }
        }
    }

    public Color TodayColor
    {
        get { return _TodayColor; }
        set
        {
            if (_TodayColor != value)
            {
                _TodayColor = value;
                OnPropertyChanged("TodayColor");
                OnPropertyChanged("IntValue");
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM