c# - 如何更改 WPF 中的 View ?

标签 c# wpf mvvm

有一些空闲时间,想尝试用 WPF 制作游戏。 我想知道,更改窗口 View 的最佳方法是什么? 我制作了一个“主菜单”窗口,其中包含三个按钮……新游戏、继续游戏和退出游戏。 当按下新游戏时,我希望窗口转到下一个“viewstate”以创建播放器等,不希望弹出新窗口。什么是实现它的最佳方式。

最佳答案

如果你想根据 ViewModel 动态切换 View ,使用 DataTemplates 是合适的:

<Window>
   <Window.Resources>
      <DataTemplate DataType="{x:Type ViewModelA}">
         <localControls:ViewAUserControl/>
      </DataTemplate>
      <DataTemplate DataType="{x:Type ViewModelB}">
         <localControls:ViewBUserControl/>
      </DataTemplate>
   <Window.Resources>
  <ContentPresenter Content="{Binding CurrentView}"/>
</Window>

如果 Window.DataContextViewModelA 的一个实例,那么 ViewA 将被显示并

Window.DataContext是ViewModelB的一个实例,那么ViewB就会显示出来。

我见过和读过的最好的例子是由 Rachel Lim 制作的. See the example .

关于c# - 如何更改 WPF 中的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33652707/

相关文章:

c# - "Request format is invalid"

.net - WPF 运行时控件创建

c# - 检测用户何时在 RichTextBox 中选择文本

c# - 在接口(interface)中实现接口(interface)属性?

c# - 在 C# 中从网络驱动器加载程序集 (dll)

c# - 在运行时使用 Mahapps.Metro 添加自己的 'accents'

wpf - INotifyPropertyChanged在Model类上

c# - 命令停止执行

c# - 访问基类中的子类...但不同

c# - 将缓动函数应用于代码背后的动画