我是WPF的新手,正在尝试进行基本导航: 我在MainWindow.xaml中使用和
<Window x:Class="TRPZ.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TRPZ"
xmlns:viewmodels="clr-namespace:TRPZ.ViewModels"
xmlns:views="clr-namespace:TRPZ.Views"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<DataTemplate x:Name="authorTemplate" DataType="{x:Type viewmodels:AuthorViewModel}">
<views:Author DataContext="{Binding}"/>
</DataTemplate>
<DataTemplate x:Name="blogTemplate" DataType="{x:Type viewmodels:BlogViewModel}">
<views:Blog DataContext="{Binding}"/>
</DataTemplate>
<DataTemplate x:Name="postTemplate" DataType="{x:Type viewmodels:PostViewModel}">
<views:Post DataContext="{Binding}"/>
</DataTemplate>
</Window.Resources>
<Grid>
<Label Content="Vlad Symonenko" HorizontalAlignment="Left" Margin="139,54,0,0" VerticalAlignment="Top"/>
<ListBox HorizontalAlignment="Left" Height="100" Margin="123,167,0,0" VerticalAlignment="Top" Width="100" SelectionChanged="ListBox_SelectionChanged"/>
<Button Content="Blog" HorizontalAlignment="Left" Margin="157,100,0,0" VerticalAlignment="Top" Width="75" Click="AuthorView_Clicked"/>
<ContentControl Content="{Binding}" />
</Grid>
</Window>
借助按钮(点击事件),我可以像
一样更改视图 private void AuthorView_Clicked(object sender, RoutedEventArgs e)
{
DataContext = new AuthorViewModel();
}
当我看到AuthorViewModel时,我无法再使用相同的逻辑深入了解1级杠杆:
private void PostButton_Clicked(object sender, RoutedEventArgs e)
{
DataContext = new PostViewModel();
}
单击按钮后,没有任何反应。 那我该怎么办呢?