2016-09-03 5 views
1

내 응용 프로그램에 2 개의 ViewModel이 있습니다. 첫 번째 (FirstPageViewModel)는 내 View의 TextBox에 표시되는 데이터를 담당합니다. 다른 클래스에 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)는 네비게이션 내 페이지 사이와 TextBlocks의 값을 변경하는 책임이있다. 나는 "NavigationViewModel"의 NavigationCommand (= 버튼 클릭)를 사용하여 TextBlocks의 값을 변경하려고한다면, 아무것도 변경되지 않습니다 : 는

는 는 는

는 "FirstPageViewModel는"같은 구현 및 세트를 포함

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; 
     } 
    } 
} 
(TextBlockSetter는에서 INotifyPropertyChanged를 구현) TextBox의 값 :

static int _roundCounter = 1; 
public TextBlockSetter _helloWorld; 

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

    } 

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

변경 사항을 올바르게 구현하는 방법에 대한 아이디어가 있습니까? 내 생각은 텍스트 상자를 변경해야 할 때 NavigationViewModel에서 FirstPageViewModel에 대한 참조를 만드는 것이 었습니다. 그러나 불행히도 내 아이디어는 잘 풀리지 못했습니다.

답변

0

내가 사용하는 단 하나의 ViewModel 그 viewmodel.Therefore의 화면 속성을 정의에 대해, 당신이 그 재산 을 설정하고 특성 변경된 경우에 이동 한 것을 완전히 그나마 이해하지만.

public static readonly DependencyProperty ScreenNameProperty = 
     DependencyProperty.Register("ScreenName", typeof(String), 
     typeof(ContentControl), new FrameworkPropertyMetadata("screen_1", new PropertyChangedCallback(ScreenNameChanged))); 

    public String ScreenName 
    { 
     get { return (String)GetValue(ScreenNameProperty); } 
     set { SetValue(ScreenNameProperty, value); } 
    } 

    private static void ScreenNameChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs) 
    { 
     ContentControl originator = dependencyObject as ContentControl; 
     if (originator != null) 
     { 
      // navigate here 
     } 
    } 

ViewModel의 속성에 ScreenName 속성을 바인딩합니다. 그러면 viewmodel에서 속성을 원하는대로 변경합니다.

+0

화면 속성을 정의하는 것은 무엇을 의미합니까? – pacours

+0

첫 번째처럼 정보를 전달하고 전달할 더 많은 페이지가 있기 때문에 2 개의 ViewModels을 가져야한다는 결론에 도달했습니다. 모든 것을 하나의 ViewModel에 넣으면 모든 것을 추적하기가 어려워집니다. – pacours

+0

나는 하나의 viewmodel에 everythink를 두어야한다는 것을 의미하지 않았다. 모든 페이지에는 기본 탐색 뷰 모델이 있으며 값에 대해 다른 뷰 모델을 정의 할 수있다. – FreeMan

관련 문제