开发者

Raise event only after dependent properties change value

I have this situation where I have several controls that are related to each other. I use two-way binding for the Current/SelectedValue of said controls, so that when a user changes the value, the change is reflected in the property of my viewmodel. The thing is, after the binding triggers the change at the setter of the changed property I need to change the other controls, based on the first, update the UI and then send only one event with the new values of all the affected properties. I was thinking of doing something in the lines of this (kind of something similar to IEditableObject):

public string PropA
{
    get{(...)}
    set
    {
        BeginEdit("PropA");
        PropA = value;
        PropB = SomeValueDependentOnA;
        PropC = SomeValueDependentOnA; 
        OnPropertyChanged("PropA");
        EndEdit("PropA");               //this is propA so send the event         
}

 public string PropB
{
    get{(...)}
    set
    {
        BeginEdit("PropB");
        PropB = value;
        PropA = SomeValueDependentOnB;
        PropC = SomeValueDependentOnB; 
        OnPropertyChanged("PropB");
        EndEdit("PropB");         
}

(...)

The begin/endedit methods wou开发者_开发百科ld not do much, just check whether the prop that started the operation is the same that calls the endedit, so further beginedits would be ignored. The EndEdit we would make sure that only in the setter of PropA can we be certain that all the properties changed and we can send the event, kind of a transaction.

Although I think this would work, I think this is kind of too much to be doing on the setter and the begin/endedit seems overkill for this simple thing.

Maybe using 2-way binding is not the best option for this case and I should do the updates from methods in code behind instead of the property setters.

Any sugestions on how to do this other way?


IMHO most 'kosher' approach here is to use DependecyProperties having conflict code inside their coerce handlers, like UI components do.

Another way is to apply logic on VM fields, not properties, and then fire UpdateTarget() for bindings.


The standard way I've seen of doing it is:

set 
{
    if (PropA != value)
    {
        PropA = value;
        OnPropertyChanged("PropA");
    }
}

That way you only trigger knock on changes when the value changes, and subsequent calls (triggered by dependent values) will not trigger more changes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜