2016-09-03 13 views
1

私のアプリケーションに2つのViewModelがあります。最初のビュー(FirstPageViewModel)は、ビュー内のテキストボックスに表示されるデータを処理します。他のViewModel(NavigationViewModelは)私のページ間のナビゲーションを担当し、テキストブロックの値を変更するには:異なるクラスにINotifyPropertyChangedを実装します

<StackPanel> 
<Button Content="SecondPage" 
     DataContext="{Binding Source={StaticResource NavigationVM}}" /// reference to App.xaml 
     Command="{Binding NavigationCommand}" 
     CommandParameter="SecondPage" /> 
<Grid DataContext="{Binding Source={StaticResource FirstPageViewModel}}"> 
    <TextBlock Text="{Binding helloWorld.Counter, UpdateSourceTrigger=PropertyChanged}"/> 
    <TextBlock Text="{Binding helloWorld.Message, UpdateSourceTrigger=PropertyChanged}"/> 
    ... 
</Grid> 

今ナビゲーションが正常に動作します。しかし、私は「NavigationViewModel」にNavigationCommand(=ボタンのクリック)を使用してテキストブロック内の値を変更しようとすると、何も変わりません: (TextBlockSetterがINotifyPropertyChangedのを実装)

public TextBlockSetter _helloWorld; 


    public NavigationViewModel() 
    { 

     _helloWorld = new TextBlockSetter(); 

    } 

    public TextBlockSetter helloWorld 
    { 
     get 
     { 
      return _helloWorld; 
     } 
     set 
     { 
      _helloWorld = value; 
     } 
    } 
private void navigationCommand(object destination) 
{ 
    switch (destination.ToString()) 
    { 
    case "SecondPage": 
     { 

     ... ///Code for page Navigation 

     helloWorld.Counter++; 
     helloWorld.Message = "done"; 
     break; 
     } 
    } 
} 

を「FirstPageViewModelは」同じ実装とのセットが含まれていますテキストボックスの値:

static int _roundCounter = 1; 
public TextBlockSetter _helloWorld; 

    public FirstPageViewModel() 
    { 
     helloWorld.Counter = _roundCounter; 
     _helloWorld = new TextBlockSetter(); 

    } 

    public TextBlockSetter helloWorld 
    { 
     get 
     { 
      return _helloWorld; 
     } 
     set 
     { 
      _helloWorld = value; 
     } 
    } 

これらの変更を正しく実装する方法を知りましたか?私の考えは、TextBoxが変更されるべきときにNavigationViewModelでFirstPageViewModelへの参照を作成することでした。しかし残念ながら私の考えはうまくいっていませんでした。

答えて

0

私は完全に理解しますが、どのような使用についてのただ一つのviewmodel とそのviewmodel.Thereforeの画面プロパティを定義し、あなたが そのプロパティを設定して、プロパティ変更イベントにナビゲートすることができますいけません。

ViewModelのプロパティにScreenNameプロパティをバインドします。次に、ビューモデルのプロパティーを変更します。

+0

スクリーンプロパティを定義するとどういう意味ですか? – pacours

+0

私は2つのViewModelsが必要であるという結論に達しました。なぜなら、最初のように情報をナビゲートして渡すページがもっとたくさんあるからです。すべてを1つのViewModelにまとめると、すべてを追跡するのが難しくなります。 – pacours

+0

私は、1つのviewmodel.everyページにeverythinkを置く必要があるということを意味していませんでした。デフォルトのナビゲーションviewmodelがあり、あなたの値に異なるビューモデルを定義できます。 – FreeMan

関連する問題