开发者

Navigate from MainView to another View

I am new to WPF and i am using WPF Model-View-ViewModel Toolkit 0.1 to get my hands on WPF.

I have a fairly simple question but i cant get my head around it.

How do i开发者_开发技巧 display new view from the menu item on the mainview?

This is how my code looks like:

MainView.xaml

<Menu DockPanel.Dock="Top">
   <MenuItem Header="_File">
      <MenuItem Command="{Binding NewPage}" Header="New Page" 
                                   InputGestureText="Ctrl-N" />
   </MenuItem>
</Menu>

MainViewModel.cs

private DelegateCommand newPageCommand;

    public ICommand NewPage
    {

        get
        {

            if (newPageCommand == null)
            {
                newPageCommand = new DelegateCommand(GoToNewPage);
            }
            return newPageCommand;
        }
    }


    private void GoToNewPage()
    {
      ???
    }

What do i write in the GoToNewPage to display the newPage.xaml?


Usually your application is run entirely in the ViewModels, and Views are used to allow users to interact with the ViewModels in a friendly manner.

In your case, the ViewModel might have a property called CurrentPage, which is bound to a ContentControl.Content in your View. To change pages, the GoToNewPage command would set the CurrentPage property to a NewPageViewModel.

This would cause the ContentControl to realize it's binding has changed, and in the process of updating the binding, it would realize that the Content has changed and it needs to use a new DataTemplate to draw that content.

<ContentControl Content="{Binding CurrentPage}">
    <ContentControl.Resources>
        <DataTemplate DataType="{x:Type local:HomePageViewModel}">
            <local:HomePageView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:NewPageViewModel}">
            <local:NewPageView />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>

I have a simple example of this here if you're interested

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜