2016-05-03 23 views
0

私は同じXAMLファイルの下に2つのviewModelを持っています。WPF - バインドと2つの異なるViewModelでのコマンド

ViewModel(1) - ビューモデル内の機能を起動するコマンドを持つボタンが含まれています。

ViewModelには、(2)は - 質問

XAML

にバインドされプロパティが含まれています:私はを使用してにViewModelに(2)をその特定のプロパティを更新することが可能ですコマンド in ViewModel(1)?はいの場合は、その方法に簡単なガイドラインを適用できますか?

要件は、私がコマンドと同じViewModelににあるようにプロパティをシフトするように変更をしないように設定します。

は、任意の助けの人たちのために事前にありがとう:)あなたはこの試みることができる

+1

ViewModel(1)からViewModel(2)を参照するだけで、コマンドが呼び出されたら、プロパティを変更しますか? – 3615

+0

ビューモデル1では、ビューモデル2のプロパティを更新するアクションを保存できます。ビューモデル1では、アクションを呼び出すことができず、ビューモデル2が変更されます。 –

+0

@ 3615こんにちは、私はちょうどそれを試した。それは期待どおりに動作します。しかし、なぜ私の場所のpplが別のviewModel内に1つのviewModelインスタンスを持たないようにしようとしているのかわかりません。 – DriLLFreAK100

答えて

1

:のviewmodel 1では

public class FirstViewModel : INotifyPropertyChanged 
{ 
    public delegate void PropertyChangedHandler(object obj); 
    public static event PropertyChangedHandler MyPropertyChanged = delegate { }; 
    public FirstViewModel() 
    { 
     //Example: here I fire the function in the second ViewModel with parameter 
     var obj = new { Name = "Jhon" }; 
     MyPropertyChanged(obj); 
    } 
} 

第二のViewModel

public class SecondViewModel : INotifyPropertyChanged 
{ 
    public SecondViewModel() 
    { 
     FirstViewModel.MyPropertyChanged += OnMyPropertyChanged; 
    } 

    public void OnMyPropertyChanged(object obj) 
    { 
     //... 
    } 

    //.... 
} 
1

を、あなたは更新アクションを保存することができますビューモデル2のプロパティ。ビューモデル1では、アクションを呼び出すことができず、ビューモデル2が変更されます。

public class SomeOtherClass 
{ 
    public void main() 
    { 
     var vm1 = new ViewModel1(); 
     var vm2 = new ViewModel2(); 

     vm1.ChangeValueAction = new Action(() => { vm2.SomeProperty = String.Empty; }); 
    } 
} 

public class ViewModel1 
{ 
    public Action ChangeValueAction { get; set; } 

    public void SomeMethod() 
    { 
     ChangeValueAction.Invoke(); 
    } 
} 

public class ViewModel2 
{ 
    public string SomeProperty { get; set; } 
} 
2

私はあなたがメッセンジャーを使うべきだと思います。
ここでは、mvvmアーキテクチャのメッセンジャーについての良い記事です。
少し古いですが、それはあなたを助けることができます:https://msdn.microsoft.com/en-us/magazine/jj694937.aspx

ここはあなたのための例です。
は、私が追加しました:たとえば、私は可能性:)すべてのhttp://www.galasoft.ch/mvvm/

まずとして最も簡単であるために、GalasoftからMVVMライトを使用し、私はあなたが放送したいあなたのメッセージに関連するエンティティを作成するためにrecommand単純な文字列プロパティが、明らかにあなたは:)

public class Vm1toVm2Message 
{ 
    public String Message { get; set; } 
} 

は、その後、あなたのVM1にあなたのメッセージを作成し、それを放送したいものは何でも追加することができます。あなたの中

public class ViewModel1 : ViewModelBase 
{ 
    private RelayCommand _refreshCommand; 
    public RelayCommand RefreshCommand 
    { 
     get 
     { 
      return _refreshCommand ?? (_refreshCommand = new RelayCommand(() => 
      { 
       // You button command code 
       // ------------------- 

       // Send a message 
       Messenger.Default.Send<Vm1toVm2Message>(new Vm1toVm2Message { Message = "Update from VM1" }); 
      })); 
     } 
    } 
} 

そして最後にVM2の場合、着信メッセージを待つ:

public class ViewModel2 : ViewModelBase 
{ 
    public ViewModel2() 
    { 
     Messenger.Default.Register<Vm1toVm2Message>(this, HandleVm1toVm2Message); 
    } 

    private void HandleVm1toVm2Message(Vm1toVm2Message msg) 
    { 
     // Do what you want here 
    } 
} 
+0

私はこれに同意しますが、答えではなくコメントでなければなりません。あなたが精緻化し、提供することができ、例がない限り。 –

+1

正しい例を提供するために編集します。 –

関連する問題