Change Notification in MVVM Hierarchies

前端 未结 5 1281
清酒与你
清酒与你 2021-02-01 07:29

Let\'s say in some abstract ViewModel base-class I have a plain-old property as follows:

public Size Size
{
    get { return _size; }
    set
    {
        _size         


        
5条回答
  •  广开言路
    2021-02-01 08:11

    You can simply override OnPropertyChanged in the derived ViewModel like so:

    protected override void OnPropertyChanged(string propertyName) {
        base.OnPropertyChanged(propertyName);
        if (propertyName == "Size") {
            base.OnPropertyChanged("Rectangle");
        }
    }
    

    Another possibility... A while back I put together a pretty nice ViewModel base class that supports attributes on properties like:

    [DependsOn("Size")]
    public Rect Rectangle {
        get { new Rect(0,0,Size.Width, Size.Height); }
    }
    

    Then the ViewModel base class collects these DependsOnAttribute's at runtime and in its OnPropertyChanged method it basically just looks to see what other properties need to be invalidated when a property change occurs.

提交回复
热议问题