내 응용 프로그램에 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에 대한 참조를 만드는 것이 었습니다. 그러나 불행히도 내 아이디어는 잘 풀리지 못했습니다.
화면 속성을 정의하는 것은 무엇을 의미합니까? – pacours
첫 번째처럼 정보를 전달하고 전달할 더 많은 페이지가 있기 때문에 2 개의 ViewModels을 가져야한다는 결론에 도달했습니다. 모든 것을 하나의 ViewModel에 넣으면 모든 것을 추적하기가 어려워집니다. – pacours
나는 하나의 viewmodel에 everythink를 두어야한다는 것을 의미하지 않았다. 모든 페이지에는 기본 탐색 뷰 모델이 있으며 값에 대해 다른 뷰 모델을 정의 할 수있다. – FreeMan