简体   繁体   English

从另一个视图更改为视图

[英]Changing to view from another view

i am currently learning C# and WPF.我目前正在学习 C# 和 WPF。 I have started coding simple application, i have 1 model class (Book), 2 model views (BookListModelView, AddNewBookModelView) and 2 views booth are UserControl (BookListView, AddNewBookView).我已经开始编写简单的应用程序,我有 1 个 model class(书籍)、2 个 model 视图(BookListModelView、AddControlNewBookListView)和 AddNewBookView 2 个视图。

My question is how can i change to AddNewBookView when my current view is BookListView?我的问题是当我当前的视图是 BookListView 时如何更改为 AddNewBookView?

MainWindow.xaml:主窗口.xaml:

<ContentControl Grid.Row="1" Grid.Column="1" Content="{Binding}"/>

MainWindow.xaml.cs: MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        BookListModelView bookListMV = new BookListModelView();
        this.DataContext = bookListMV;
    }
}

BookListView.xaml.cs BookListView.xaml.cs

public partial class BookListView : UserControl
{
    public BookListView()
    {
        InitializeComponent();
    }

    private void NewBookBtn_Click(object sender, RoutedEventArgs e)
    {
        this.DataContext = new AddNewBookViewModel();
    }
}

Thank you for your answers!谢谢您的回答!

The usual way to achieve this is with DataTemplates.实现这一点的常用方法是使用 DataTemplates。 Basically you have a property in your main view model containing the view models of your child page:基本上,您的主视图 model 中有一个属性,其中包含子页面的视图模型:

public object PageViewModel {get; set;} // <--- needs property change notification

I've used object as the type here, whereas in practice you typically have some base class for your pages.我在这里使用了object作为类型,而在实践中,您通常有一些基本的 class 用于您的页面。

Over in your view you create a ContentControl and bind it to this property:在您的视图中,您创建了一个 ContentControl 并将其绑定到此属性:

<ContentControl Content="{Binding PageViewModel}" />

Finally, you need to specify some kind of mapping specifying which view to display for each view model type.最后,您需要指定某种映射,为每个视图 model 类型指定要显示的视图。 That's where data templates come in:这就是数据模板的用武之地:

<DataTemplate DataType="{x:Type local:ChildViewModel1}">
    <local:ChildViewControl1 />
</DataTemplate>

<DataTemplate DataType="{x:Type local:ChildViewModel2}">
    <local:ChildViewControl2 />
</DataTemplate>

Another way to achieve this is via Data Triggers.实现此目的的另一种方法是通过数据触发器。 These get put in the parent's style and check for each possible value, setting the content explicitly:这些被放入父母的风格并检查每个可能的值,明确设置内容:

<DataTrigger Binding="{Binding PageViewModelProperty}" Value="SomeChildViewModel1Value">
    <Setter Property="Template" Value="{StaticResource MyPageTemplate1}" />
</DataTrigger>

<DataTrigger Binding="{Binding PageViewModelProperty}" Value="SomeChildViewModel2Value">
    <Setter Property="Template" Value="{StaticResource MyPageTemplate2}" />
</DataTrigger>

... and so on. ... 等等。 This is a better fit in cases where you may not have a separate class type for each of your page types eg in the case of page containing content that is generated dynamically.这更适合您可能没有针对每种页面类型的单独 class 类型的情况,例如在页面包含动态生成的内容的情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM