MVVM - binding to aggregated property

后端 未结 1 755
再見小時候
再見小時候 2021-01-24 15:40

I have following viewmodels:

public class ViewModel: INotifyPropertyChanged
{
    public ObservableCollection Items { get; set; }
    ...
}
public cl         


        
相关标签:
1条回答
  • 2021-01-24 15:47

    The data binding approach seems easiest in this particular case. That way you rely upon WPF's data binding mechanism handling the event subscriptions for you, rather than having to do it manually via code.

    The only other way I know of handling this is to use some kind of mediator object. When the Valid property of SubItem is changed you send out a message that this has happened. Your Item class, which has subscribed to this message, handles it by checking the current valid state of A and B, then sets its own Valid property accordingly.

    This approach is not without its own wrinkles however. For one thing, you need to inject the mediator object into your ViewModel objects. Also your Item objects need to subscribe to and unsubscribe from the relevant message at the appropriate times (usually on object creation and destruction). All of this plumbing, while still easier than handling property changed events directly, is more difficult than just using a data trigger and relying on WPF's binding mechanism IMO.

    0 讨论(0)
提交回复
热议问题