【问题标题】:Notify child class property changed通知子类属性已更改
【发布时间】:2019-10-15 11:51:45
【问题描述】:

是否可以通知子类的更改?就像更改 ValueA 时通知 ValueB 绑定的方式一样?

PropertyChangedEventHandler 只允许通知属性名称。

我看到的唯一方法是向 Child 类添加功能以在那里调用通知(Notify 方法)..

public class Parent: INotifyPropertyChanged
{
    public Child ChildA
    {
        get; set;
    }

    public Child ChildB
    {
        get; set;
    }

    public int ValueA
    {
        get
        {
            return _valueA;
        }
        set
        {
            _valueA = value; 
            OnPropertyChanged(nameof(ValueA));
        }
    }

    public int ValueB
    {
        get
        {
            return _valueB;
        }
        set
        {
            _valueB = value; 
            OnPropertyChanged(nameof(ValueA));
            OnPropertyChanged(nameof(ValueB));
        }
    }

    public void RefreshBindings()
    {
        OnPropertyChanged(ChildA.Check);
        OnPropertyChanged(ChildB.Check);
    }
}

public class Child: INotifyPropertyChanged
{
    public void Notify(string property)
    {
        OnPropertyChanged(property);
    }

    public bool Check
    {
        get
        {
            return // something;
        }
    }
}

【问题讨论】:

  • 是的,你也应该在 Child 类上实现 INotifyPropertyChanged
  • 如果/当您将属性名称留空时,整个对象将被“刷新”
  • 你在视图中绑定了哪个属性?
  • @NawedNabiZada: nameof(ChildA.Check) 等于 Check
  • @NawedNabiZada 感谢您的建议,但它们不起作用。请仅在您知道它们有效的事实时才建议它。

标签: c# wpf inotifypropertychanged


【解决方案1】:

不,应该是绑定的实现INotifyPropertyChanged 接口并为框架引发更改通知,以便能够“自动”刷新绑定。

因此,如果您绑定到 Parent 中的 ChildA.Check,那么应该实现 INotifyPropertyChanged 的是由 ChildA 属性(即 Child 类)返回的对象。

另一种选择是绑定到包装Child 属性的Parent 的属性,但Child 仍必须在其状态更改时以某种方式通知父级。

【讨论】:

  • 我真的很想用 RefreshBindings 方法来通知子类的变化。
  • Check 属性只有一个 getter,所以我必须从其他地方通知。我的问题中的代码是我实际代码的简化版本。
  • 绑定到包装器属性X,然后返回ChildA.Check 并为X 引发PropertyChanged 事件。
  • 可以,但这不是我想要的。
  • 那你在找什么?这就是绑定引擎的工作方式,您对此无能为力。
【解决方案2】:

@NawedNabiZada 感谢您的建议,但它们不起作用。 请仅在您知道它们有效的情况下才建议它。

不确定你尝试了什么,但我的意思是:

<Grid>
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="Child A :"/>
            <Label Content="{Binding Path=ChildA.Check}"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="Child B :"/>
            <Label Content="{Binding Path=ChildB.Check}"/>
        </StackPanel>

        <Button Content="Check/UnCheck" Command="{Binding Path=RefreshBindingCommand}"/>
    </StackPanel>
</Grid>

家长:

public class Parent : INotifyPropertyChanged
{
    public Child ChildA
    {
        get; set;
    }

    public Child ChildB
    {
        get; set;
    }

    public ICommand RefreshBindingCommand { get; }

    public Parent()
    {
        ChildA = new Child(true);
        ChildB = new Child(false);
        RefreshBindingCommand = new RelayCommand(RefreshBindingCommand_Execute);
    }

    void RefreshBindingCommand_Execute(object obj)
    {
        RefreshBindings();
    }

    public void RefreshBindings()
    {
        ChildA.Notify(nameof(ChildA.Check));
        ChildB.Notify(nameof(ChildB.Check));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

孩子:

public class Child : INotifyPropertyChanged
{
    private bool _check;    

    public bool Check
    {
        get
        {
            _check = !_check;
            return _check;
        }
    }

    public Child(bool check)
    {
        _check = check;
    }

    public void Notify(string property)
    {
        OnPropertyChanged(property);
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

证明它有效:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 2013-12-10
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多