我正在使用 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下面是我代碼的重要部分:主窗口.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>
1 回答

阿波羅的戰車
TA貢獻1862條經驗 獲得超6個贊
Route通過INotifyPropertyChanged接口實現發送有關屬性更改的通知:
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(){}
}
此類通知將激活 DataTrigger,后者又將更改 ContentTemplate
- 1 回答
- 0 關注
- 190 瀏覽
添加回答
舉報
0/150
提交
取消