2013-02-18 92 views
5

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

これは、メニューバーから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; 
      } 
     } 

ありがとうございました。 Roshil K

+0

をあなたが記述されているシナリオのためのいくつかのサンプルコードを提供することができるだろう、このヘルプ:それは次のようにすることができますか? MyListが既にDatagridにバインドされている場合、リストの更新によってグリッドが自動的に更新されます。 MyListがパブリックプロパティであり、開いているファイルのVMがメインのVMへの参照を持っている場合は、そのファイルを直接読み込むことができます。 – Dutts

+0

'OpenFile'ビューをどうやって開きますか? –

+0

私はメニューバーからOpenFile Windowを開いています。 –

答えて

3

後私はメインビューモデルのCurrentインスタンスを次のコードで取得しました。

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

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

1

は、最も簡単な方法は、OpenFileViewModelMainWindowViewModelのインスタンスを渡すことです。その後

public class OpenFileViewModel 
{ 
    private MainWindowViewModel _parent; 

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

あなたがMainWindowViewModel内の任意のパブリックメソッド/プロパティにアクセスする/呼び出すことができます。

foreach (var item in _parent.myList) 
{ 
    ... 
} 
1

おかげであなたは「メディエータ・サービス」にすることができ、それがあなたのviewmodels間座ってクラスを作成することができます。メディエータサービスを登録し、ある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 
} 

希望あなたは...

関連する問題