通知子类属性已更改

时间:2019-10-15 11:51:45

标签: c# wpf inotifypropertychanged

是否可以通知子类的更改?就像更改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;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

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

因此,如果您绑定到Parent的{​​{1}},则ChildA属性(即Child类)返回的对象应实现INotifyPropertyChanged

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

答案 1 :(得分:1)

  

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

证明有效: enter image description here