WPF mvvm routes



我正在使用WPF在C#中创建桌面应用程序,但我被我的菜单困住了。 我有一个左侧面板菜单可以在功能之间切换。

我创建了一个具有 2 个视图的视图模型,这些视图需要显示在我的主窗口中。 如果我单击"监视",则内容视图将从"主页"切换到"监视",但是当我处于"监视"状态时,如果我尝试返回"主页",它不起作用,我不明白为什么。

在我的项目文件夹下方

([abc] = 文件夹; - abc = 文件( :

[PROJECT]   
[assets]
- Home.png
- Stats.png
[ViewModels]
- MainWindowViewModel.cs
[Views]
- Home.xaml
- Home.cs
- Stats.xaml
- Stats.cs
- MainWindow.xaml
- MainWindow.cs
- App.xaml
- App.cs
- App.config

以下是我代码的重要部分:

MainWindow.xaml

<Window.Resources>
<DataTemplate x:Key="HomeViewTemplate" DataType="{x:Type viewmodels:MainWindowViewModel}">
<views:Home DataContext="{Binding}" />
</DataTemplate>
<DataTemplate x:Key="StatsViewTemplate" DataType="{x:Type viewmodels:MainWindowViewModel}">
<views:Stats DataContext="{Binding}" />
</DataTemplate>
</Window.Resources>
<Grid Grid.Row="0" MouseLeftButtonDown="OpenHome">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Image Source="/assets/home.png" Height="32" Width="32" />
<Label Content="Home" Grid.Column="1" Foreground="White" FontSize="12" VerticalAlignment="Center" Padding="0,5,5,5" />
</Grid>
<Grid Grid.Row="1" MouseLeftButtonDown="OpenStats">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Image Source="/assets/stats.png" Height="32" Width="32" />
<Label Content="Monitoring" Grid.Column="1" Foreground="White" FontSize="12" VerticalAlignment="Center" Padding="0,5,5,5" />
</Grid>
<Grid Grid.Row="1">
<ContentControl Content="{Binding }">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource HomeViewTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Route}" Value="Home">
<Setter Property="ContentTemplate" Value="{StaticResource HomeViewTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Route}" Value="Stats">
<Setter Property="ContentTemplate" Value="{StaticResource StatsViewTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</Grid>

主窗口.cs

private MainWindowViewModel MV = new MainWindowViewModel();
public MainWindow()
{
InitializeComponent();
}
private void OpenHome(object sender, RoutedEventArgs e)
{
MV.Route = "Home";
DataContext = MV;
}
private void OpenStats(object sender, RoutedEventArgs e)
{
MV.Route = "Stats";
DataContext = MV;
}

视图模型>主窗口视图模型.cs

public class MainWindowViewModel
{
public string Route
{
get;
set;
}
public MainWindowViewModel(){}
}

通过INotifyPropertyChanged接口实现发送有关Route属性更改的通知:

public class MainWindowViewModel : INotifyPropertyChanged
{
public string _Route;
public string Route
{
get { return _Route; }
set { _Route = value; OnPropertyChanged("Route"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public MainWindowViewModel(){}
}

此类通知将激活数据触发器,进而更改内容模板

相关内容

  • 没有找到相关文章

最新更新