6

MVVMライトツールキットを使用してWPFアプリケーションを開発しています。メインウィンドウにデータグリッドがあります。「openfile」という名前の別のウィンドウとそのビューモデルを作成しました。メインウィンドウのviewmodelクラスには、データグリッドにバインドされたObservableCollectionMyListタイプのパブリックプロパティが含まれています。オープンファイルのViewmodelからこのプロパティを入力して、Datagridに自動的にバインドできますか?または、変数をMainViewmodelに渡して、OpenfileViewmodelからMainViewmodelのパブリック関数を呼び出すことはできますか?

これが、メニューバーからMyPageを呼び出す方法です。

 private void NotificationMessageReceived(NotificationMessage msg)
        {
            switch (msg.Notification)
            {
                case Messages.MainVM_Notofication_ShowNewbWindow:
                    new NewView().ShowDialog();
                    break;
                case Messages.MainVM_Notofication_ShowExistingWindow:
                    new OpenExisitingView().ShowDialog();
                    break;

                case Messages.MainVM_Notofication_ShowotherWindow:
                    newView().ShowDialog();
                    break;
            }
        }

前もって感謝します。ロシルK

4

3 に答える 3

4

少し調べた後、次のコードでMainviewmodelの現在のインスタンスを取得しました。

MainViewModel mainViewModelInstaince = ServiceLocator.Current.GetInstance<MainViewModel>();

次に、すべてのメソッドとプロパティを取得し、別のビューモデルからデータをバインドしました。

ありがとうございます..

于 2013-02-18T11:36:59.653 に答える
2

「メディエーター サービス」となるクラスを作成すると、ViewModel の間に配置されます。メディエーター サービスを登録し、ある VM から発生させて別の VM で処理できるイベントを追加できます。次のようになります。

public class MediatorService: IMediatorService 
{
  public dynamic Data { get; set;}
  public event EventHandler<YourCustomEventArgs> Callback = delegate { }
}

public class XYZVM(IMediatorService mediatorService)
{
// set your Data here and handle Callback event here and refresh your grid.
// you can get anything from your "YourCustomEventArgs" which you will set from ABCVM
}

public class ABCVM(IMediatorService mediatorService)
{
// get your data here and raise callback here and handle that in XYZVM
}

これがあなたを助けることを願っています..

于 2013-02-18T18:06:42.930 に答える
1

MainWindowViewModel最も簡単な方法は、のインスタンスをに渡すことOpenFileViewModelです。

public class OpenFileViewModel
{
    private MainWindowViewModel _parent;

    public OpenFileViewModel(MainWindowViewModel parent)
    {
          _parent = parent;
    }
}

その後、で任意のパブリック メソッド/プロパティを呼び出し/アクセスできますMainWindowViewModel

foreach (var item in _parent.myList)
{
    ...
}
于 2013-02-18T10:27:48.510 に答える