WPF MVVM如何从另一个viewmodel更新viewmodel

时间:2018-02-11 13:53:26

标签: c# wpf mvvm

我尝试实现WPF MVVM项目。 我有2个从ViewModelBase继承的ViewModel

ViewModelBase

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    internal void RaisePropertyChanged(string prop)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }

    bool? _CloseWindowFlag;
    public bool? CloseWindowFlag
    {
        get
        {
            return _CloseWindowFlag;
        }
        set
        {
            _CloseWindowFlag = value;
            RaisePropertyChanged("CloseWindowFlag");
        }
    }

    public virtual void CloseWindow(bool? result = true)
    {
        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
        {
            CloseWindowFlag = CloseWindowFlag == null
                              ? true
                              : !CloseWindowFlag;
        }));
    }
}

MainWindowViewModel

class MainWindowViewModel : ViewModelBase
{

    //The Response_info property.
    private ObservableCollection<Response_info_type> responseInfo;
    public ObservableCollection<Response_info_type> ResponseInfo
    {
        get
        {
            return responseInfo;
        }
        set
        {
            if (value != null)
            {
                responseInfo = value;
                RaisePropertyChanged("Response_info");
            }
        }
    }

}

** MyViewModel **

class MyViewModel :  ViewModelBase
{
    public string myString
    {
        get
        {
         return ResponseInfo.Parametrt.ToString();
        }
        set{ ;}
    }

    // ctor
    public MyViewModel ()
    {

    }
}

我希望在更新ResponseInfo时,myString也会更新。 当我更新ResponseInfo时,我得到了RaisePropertyChanged(字符串道具),但我不知道如何在MyViewModel中实现此事件。

0 个答案:

没有答案