简体   繁体   English

通知子 class 属性更改

[英]Notify child class property changed

Is it possible to notify changes on a child class?是否可以通知孩子 class 的更改? Like the way binding on ValueB is notified when changing ValueA?就像更改 ValueA 时通知 ValueB 绑定的方式一样?

The PropertyChangedEventHandler only allows a propertyname to be notified. PropertyChangedEventHandler 只允许通知属性名称。

The only way I see is adding functionality to the Child class to call notification there (Notify method)..我看到的唯一方法是向子 class 添加功能以在那里调用通知(通知方法)..

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

No, it's the source of the binding that should implement the INotifyPropertyChanged interface and raise change notifications for the framework to be able to refresh the bindings "automatically".不,它是绑定的,它应该实现INotifyPropertyChanged接口并引发框架的更改通知,以便能够“自动”刷新绑定。

So if you bind to ChildA.Check of Parent , it's the object returned by the ChildA property (ie the Child class) that should implement INotifyPropertyChanged .因此,如果您绑定到ChildA.CheckParent ,则应该实现INotifyPropertyChangedChildA属性(即Child类)返回的 object 。

The other option would to bind to properties of Parent that wraps properties of Child , but the Child must still somehow notify the parent when its state changes.另一种选择是绑定到包装了Child属性的Parent的属性,但是当它的 state 发生变化时, Child仍然必须以某种方式通知父级。

@NawedNabiZada I appreciate you suggestions but they do not work. @NawedNabiZada 感谢您的建议,但它们不起作用。 Please only suggest it if you know for a fact they work.请仅在您知道它们有效的事实时才建议它。

Not sure what you tried, but my point is this:不确定您尝试了什么,但我的观点是:

<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>

Parent:家长:

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

Child:孩子:

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

Proof that it works:证明它有效: 在此处输入图像描述

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

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