c# - 单击按钮更改 View

标签 c# wpf user-interface mvvm

我目前正在学习 WPF 和 MVVM(或者至少我正在尝试...)。

我创建了一个小示例应用程序,它显示了一个带有 2 个按钮的窗口,每个按钮都应在单击时显示一个新 View 。所以我创建了 3 个 UserControl(带有 2 个按钮的 DecisonMaker,每个“clicktarget”都有一个 Usercontrol)。

所以我将 MainWindow 的 CotentControl 绑定(bind)到我的 MainWindowViewModel 中名为“CurrentView”的属性

MainWindow.xaml代码:

<Window x:Class="WpfTestApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfTestApplication"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:MainWindowViewModel />
</Window.DataContext>
<Grid>
    <ContentControl Content="{Binding CurrentView, Mode=OneWay}" />
</Grid>
</Window>

MainWindowViewModel 代码:

class MainWindowViewModel
{      
    private UserControl _currentView = new DecisionMaker();
    public UserControl CurrentView
    {
        get { return _currentView; }
        set { _currentView = value; }
    }

    public ICommand MausCommand
    {
        get { return new RelayCommand(LoadMouseView); }
    }

    public ICommand TouchCommand
    {
        get { return new RelayCommand(LoadTouchView); }
    }

    private void LoadMouseView()
    {
        CurrentView = new UserControlMouse();
    }

    private void LoadTouchView()
    {
        CurrentView = new UserControlTouch();
    }
}

最初的 UserControl (DecisionMaker) 按预期显示。还调用了方法 LoadMouseView。但是 View 不会改变。我错过了什么?

更新:非常感谢!我错过了 INotifyPropertyChanged 接口(interface)。您的所有答案都很棒,非常准确且很有帮助!我不知道该接受哪个 - 我认为接受“第一个”答案是最公平的方式?

我接受了盲人的回答,因为它解决了问题并帮助我更好地理解了 MVVM。但是每一个答案都非常感谢你们所有人!

最佳答案

如果你想做 mvvm - 那么你应该在你的 View 模型中没有引用你的 View /用户控件。你必须实现 INotifyPropertyChanged! ps:如果您的 Viewmodel 中需要 System.Windows 命名空间 - 那么就会出现问题。

在你的情况下你需要什么:

  • 1个主视图模型
  • 1 个用于 UserControlMouse 的 View 模型
  • 1 个用于 UserControlTouch 的 View 模型
  • 1 个用于 UserControlMouse 的 View /用户控件
  • 1 个用于 UserControlTouch 的 View /用户控件

您的主视图模型应该至少有 2 个命令来切换 View 和 1 个 CurrentView 属性。在您的命令中,您只需将 CurrentView 设置为正确的 View 模型实例。至少您需要为每个定义正确 View 的 View 模型定义两个数据模板。

public object CurrentView
{
    get { return _currentView; }
    set {
        _currentView = value; this.RaiseNotifyPropertyChanged("CurrentView");}
}

xaml

<Window x:Class="WpfTestApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTestApplication"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
 <DataTemplate DataType="{x:Type local:MyMouseViewModel}">
   <local:MyMouseUserControlView/>
  </DataTemplate>
 <DataTemplate DataType="{x:Type local:MyTouchViewModel}">
   <local:MyTouchUserControlView/>
  </DataTemplate>
</Window.Resources>
<Window.DataContext>
 <local:MainWindowViewModel />
</Window.DataContext>
<Grid>

 <!-- here your buttons with command binding, i'm too lazy to write this. -->

 <!-- you content control -->
 <ContentControl Content="{Binding CurrentView, Mode=OneWay}" />
</Grid>
</Window>

关于c# - 单击按钮更改 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10993385/

相关文章:

DataGrid 周围的 WPF ScrollViewer 影响列宽

android - 为什么所有属性都有前缀?

c# - 鼠标滚轮上的滚动窗体

c# - 哪种设计方案更适合施工时的自动校正?

c# - 如何将枚举转换为 C# 中的列表?

javascript - 何时不使用 gridview

android - 如何像Android的Toast一样在Blackberry中显示快速消息?

c# - 将参数传递给 WebClient.DownloadFileCompleted 事件

c# - 使用 CEFSharp Web 浏览器的 WPF 应用程序在 clickonce 版本中崩溃

c# - WPF - 水平滚动条拇指宽度