属性已更改设置旧值



我有一个树视图,当某些项目标签更改时,它应该应用于所有子项。我使用以下方法将标签应用于儿童:

    public void SetCLassificationForChildren(TreeItemViewModel item, Labels label)
    {
        if (item == null) return;
        item.Label = label;
        item.Children?.ForEach(c => SetCLassificationForChildren(c, label));
    }

下面是一个 Label-property 和 INotifyPropertyChanged 实现:

...
    public Labels Label
    {
        get => _label;
        set
        {
            _label = value;
            OnPropertyChanged(() => Label);
        }
    }
...
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
    {
        var body = propertyExpression.Body as MemberExpression;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(body.Member.Name));
    }

因此,当属性获得标签值时,将执行以下操作:

_label = 值 - 工作正常

但是,在那之后是以下行:

OnPropertyChanged((( => Label( 再次引发设置代码并设置旧值。

我不知道为什么会这样。

UPD"标签"是我的枚举类型:

public enum Labels
{
    NotClassified = 0,
    Internal,
    Confidential,
    StrictlyConf
}

问题与转换器有关:

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || !(bool)value) return null;
        string btnLabelString = parameter as string;
        Labels btnLabel;
        switch (btnLabelString)
        {
            case "internal":
                btnLabel = Labels.Internal;
                break;
            case "confidential":
                btnLabel = Labels.Confidential;
                break;
            case "strictlyConf":
                btnLabel = Labels.StrictlyConf;
                break;
            default:
                btnLabel = Labels.NotClassified;
                break;
        }
        return btnLabel;
    }

它返回了旧值,但我添加了

if (值 == 空 || !(布尔(值(返回空值;

并在二传手中处理了案件:

    public Labels Label
    {
        get => _label;
        set
        {
            if(value != null)
            {
                _label = value;
                OnPropertyChanged(() => Label);
            }
        }
    }

Label-property 和 INotifyPropertyChanged 实现。

private Labels _label;
public Labels Label
{
   get { return _label; }
   set
        {
            if (value != null)
            {
                _label = value;
                OnPropertyChanged("Label");
            }
        }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = this.PropertyChanged;
    if (handler != null)
    {
        var e = new PropertyChangedEventArgs(propertyName);
        handler(this, e);
    }
}
protected void OnPropertyChanged<T>(Expression<Func<T>> propertySelector)
{
    var propertyChanged = PropertyChanged;
    if (propertyChanged != null)
    {
        string propertyName = GetPropertyName(propertySelector);
        propertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

相关内容

  • 没有找到相关文章

最新更新