C# 在不同类中实现INotifyPropertyChanged
我的应用程序中有2个ViewModels。第一个(FirstPageViewModel)负责在我的视图的文本框中显示的数据。另一个ViewModel(NavigationViewModel)负责在我的页面之间进行导航,并更改文本块的值:C# 在不同类中实现INotifyPropertyChanged,c#,mvvm,C#,Mvvm,我的应用程序中有2个ViewModels。第一个(FirstPageViewModel)负责在我的视图的文本框中显示的数据。另一个ViewModel(NavigationViewModel)负责在我的页面之间进行导航,并更改文本块的值: <StackPanel> <Button Content="SecondPage" DataContext="{Binding Source={StaticResource NavigationVM}}" /// refere
<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>
“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;
}
}
有人知道如何正确实施这些更改吗?我的想法是在NavigationViewModel中引用FirstPageViewModel,此时文本框应该更改。但不幸的是,我的想法没有一个奏效。我不完全理解,但是,只使用一个视图模型怎么样 并在该viewmodel中定义一个screen属性 该属性,然后在属性更改事件中导航
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
}
}
将ScreenName属性绑定到viewmodel中的一个属性。然后根据需要更改viewmodel中的属性。我不完全理解,但是,只使用一个viewmodel怎么样 并在该viewmodel中定义一个screen属性 该属性,然后在属性更改事件中导航
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
}
}
将ScreenName属性绑定到viewmodel中的属性。然后根据需要更改viewmodel中的属性。定义screen属性是什么意思?我得出结论,我应该有两个viewmodel,因为有更多的页面可以像第一个页面一样导航和传递信息。将所有内容放在一个ViewModel中会使跟踪所有内容变得困难。我不是说你应该将所有内容放在一个ViewModel中。每个页面都有默认的导航ViewModel,你可以为你的值定义不同的ViewModel。定义screen属性是什么意思?我得出的结论是我应该有两个ViewModel因为有更多的页面可以像第一个页面一样导航和传递信息。将所有内容放在一个ViewModel中会使跟踪所有内容变得困难。我不是说你应该将所有内容放在一个ViewModel中。每个页面都有默认的导航ViewModel,你可以为你的值定义不同的ViewModel。