亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WPF mvvm 路由

WPF mvvm 路由

C#
蠱毒傳說 2021-12-25 16:40:32
我正在使用 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


查看完整回答
反對 回復 2021-12-25
  • 1 回答
  • 0 關注
  • 190 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號