How to change view in WPF?

后端 未结 2 667
青春惊慌失措
青春惊慌失措 2021-01-13 16:10

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

相关标签:
2条回答
  • 2021-01-13 16:25

    You could implement every gui you need as an UserControl and load the needed UserControl depending on your current step.

    0 讨论(0)
  • 2021-01-13 16:46

    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>
    

    If Window.DataContext is an instance of ViewModelA, then ViewA will be displayed and

    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.

    0 讨论(0)
提交回复
热议问题