0

Structure of Program

Currently I am using the MVVMLight 4.1 framework in my application.

I have a view model XViewModel which wraps around an instance of XClass, X. X contains many properties such as S. I also have another instance of XClass in another ViewModel.

ViewModel

public XViewModelClass XViewModel : ViewModelBase
{
    public XClass X
    {
        get
        {
            return x;
        set
        }
            if(value == x)
            {
                return;                 
            }
            var oldValue = x;
            x = value;
            RaisePropertyChanged(XPropertyName, oldValue, x, true)
        }

    }
    private XClass x;
    public const string XPropertyName = "X"
}

ViewModel2

public YViewModelClass YViewModel : ViewModelBase
{
    public YViewModel()
    {
        Messenger.Default.Register<PropertyChangedMessage<XClass>>(this, message =>
        {
            X2 = message.NewValue
        });
    }

    public XClass X2
    {
        get
        {
            return x2
        set
        }
            if(value == x2)
            {
                return;                 
            }
            var oldValue = x2
            x2= value;
            RaisePropertyChanged(X2PropertyName)
        }

    }
    private string x2;
    public const string XPropertyName = "X2" 


}

Model

public class X : ObservableObject
{
    public string S
    {
        get
        {
            return s;
        set
        }
            if(value == S)
            {
                return;                 
            }

            s = value;
            RaisePropertyChanged(SPropertyName)
        }

    }
    private string s;
    public const string XPropertyName = "S" 
}

Problem

How do I ensure that when any property in X changes (e.g. S is set to a different value), RaisePropertyChanged is called for X. It would be best if I don't have to send a property changed message for every property in my model.

The reason behind is that I have another instance of XClass, `X2' in another ViewModel and I want to keep both instances in sync.

4

1 回答 1

0

普罗西莫,

在设置子成员时,您能否提供有关通过为 X 触发 PropertyChangedEvent 来完成的工作的更多信息?通常在 WPF 中,您只关心为绑定到的属性/支持字段引发该事件,在这种情况下 XS 当 X 的成员设置在我提供的场景之外时,是否有特定的用例通知?

于 2013-05-01T03:56:56.483 回答