如何改变在WPF看法? [英] How to change view in WPF?
问题描述
有一些空闲时间,想尝试做一个游戏在WPF。
我想知道,是什么改变窗口的视图的最佳方式?
我已经做了主菜单窗口中,有三个按钮。新游戏,继续游戏并退出游戏。
按新的游戏当我想要的窗口去下一个视图状态创造的球员,这样的,不想要一个新的窗口弹出。什么是实施的最佳方式。
Have some free time and wanted to try making a game in WPF. I was wondering, what is the best way of changing the view of a window? I have made a "main menu" window, with three buttons.. New Game, Continue Game and Exit Game. When pressing New Game I want the window to go to next "viewstate" for creation of player and such, dont want a new window to pop up. Whats the best way of implementing that.
推荐答案
这是合适的,如果你想动态切换取决于视图模型视图使用的DataTemplates:
It is appropriate to use DataTemplates if you want to dynamically switch Views depending on the ViewModel:
<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.DataContext
是一个实例 ViewModelA
,那么 ViewA
将显示以及
If Window.DataContext
is an instance of ViewModelA
, then ViewA
will be displayed and
Window.DataContext
是ViewModelB的一个实例,然后ViewB就会显示出来。
Window.DataContext
is an instance of ViewModelB, then ViewB will be displayed.
The best example I've ever seen and read it is made by Rachel Lim. See the example.
这篇关于如何改变在WPF看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!