使用绑定更改网格中的用户控件 [英] Use binding to change usercontrol in a grid

查看:25
本文介绍了使用绑定更改网格中的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 WPF 应用程序,它有一个主屏幕,在其中加载了其他屏幕.

I'm creating a WPF application, which has a main screen in which other screens are loaded.

主屏幕可以被看作是一个页眉/页脚模板,根据我当时需要显示的内容替换中心部分.(这是一个 3 行网格,其中 usercontrol 应该放在第 1 行).这有一个 viewmodel 正在改变页眉/页脚状态.

The main screen can be seen as a header/footer template where the central part is replaced depending on what I need to show at that moment. (It's a 3 row grid where the usercontrol should be put in row 1). This has a viewmodel that is changing header/footer status.

我已经将那些变化的部分(第 1 行)实现为用户控件.

I have implemented those changing parts (row 1) as usercontrols.

现在我想知道是否有任何方法可以使用绑定(对某些东西..),以便我可以更改加载的 usercontrol 而不是像我现在所做的那样从代码后面进行.

Now I am wondering if there is any way to use binding (to something..) so that I can change the loaded usercontrol and not do it from code behind as I'm doing it now.

如果这完全违背了 MVVM 原则,请给我提示我应该如何处理.

If this is going completely against MVVM principles, please give me hints on how I should handle this.

谢谢!!

推荐答案

在我看来,您应该考虑使用 WPF 的 DataTemplates.

It sounds to me like you should be looking at using WPF's DataTemplates.

根据您的问题以这个 XAML 示例为例:

Take this example XAML based on your question:

<Grid>
    ... XAML for your Header and Footer
    <ContentPresenter Content="{Binding MainContent">
        <ContentPresenter.Resources>
            <DataTemplate DataType="{x:Type viewmodel:UserControlViewModel}">
                <view:UserControlView>
            </DataTemplate>
        </ContentPresenter.Resources>
    </ContentPresenter>
</Grid>

在这里,我使用 ContentPresenter 作为您的内容的占位符.这绑定到主视图模型上名为 MainContent 的属性.在您的视图模型中,您可以将 MainContent 设置为您的用户控件的视图模型.

Here I'm using a ContentPresenter to act as a placeholder for your content. This is bound to a property called MainContent on your main viewmodel. In your viewmodel, you could then set the MainContent to be the viewmodel for your usercontrol.

ContentPresenter 将尝试显示它绑定到的任何对象.在此示例中,我创建了一个 DataTemplate,它将特定对象类型(例如 UserControlViewModel)映射到视图(例如 UserControlView).

The ContentPresenter will attempt to display whatever object it is bound to. In this example, I've created a DataTemplate that maps a specific object type (e.g. UserControlViewModel) to a view (e.g. UserControlView).

您可以定义多个 DataTemplates,以允许您的内容展示器处理多个视图模型(或任何类型的对象):

You can define multiple DataTemplates, to allow your content presenter to handle multiple viewmodels (or any type of object):

<Grid>
    ... XAML for your Header and Footer
    <ContentPresenter Content="{Binding MainContent">
        <ContentPresenter.Resources>
            <DataTemplate DataType="{x:Type viewmodel:UserControlViewModel}">
                <view:UserControlView>
            </DataTemplate>
            <DataTemplate DataType="{x:Type viewmodel:AnotherUserControlViewModel}">
                <view:AnotherUserControlView>
            </DataTemplate>
        </ContentPresenter.Resources>
    </ContentPresenter>
</Grid>

我希望这为您指明了正确的方向.

I hope that points you in the right direction.

您可以在 MSDN 上找到有关数据模板的更多信息:

You can find out more info on Data Templating on MSDN:

https://msdn.microsoft.com/en-us/library/ms742521(v=vs.110).aspx

值得一提的是,您可以在主视图模型中新建 UserControl.xaml,并将其设置为 MainContent 属性.那里不需要 DataTemplate,因为 ContentPresenter 知道如何呈现 UserControl.但是,这与 MVVM 背道而驰,因为您将 UI (XAML) 与代码(视图模型)紧密耦合.

It's worth mentioning that you COULD just new-up your UserControl.xaml in your main viewmodel, and set that to the MainContent property. No DataTemplate would be required there, as the ContentPresenter knows how to present a UserControl. However, this goes against MVVM, as you're tightly coupling your UI (XAML) with your code (viewmodel).

这篇关于使用绑定更改网格中的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆