从 MainWindow (WPF/MVVM) 访问 UserControl 中的属性 [英] Accesing properties in a UserControl from the MainWindow (WPF/MVVM)

查看:46
本文介绍了从 MainWindow (WPF/MVVM) 访问 UserControl 中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,我正在寻找实现它的正确方法.

I have a little problem and I'm looking the correct way to implement it.

我有一个 MainWindow 和一个 UserControl 来显示一些结果,在 MainWindow 我有一个按钮加载"来加载一些数据,UserControl 应该显示它们.

I have a MainWindow and a UserControl to display some result, in the MainWindow I have a button "load" to load some data and the UserControl should display them.

我不确定在 WPF 和 MVVM 中执行此操作的正确方法是什么:

I'm not sure what is the correct way to do this in WPF and MVVM:

我应该将 MainWindowModel 传递给 UserControlModel 吗?;

Should I pass the MainWindowModel to the UserControlModel?;

我应该将 UserControlModel 传递给 MainWindowModel 吗?;

Should I pass the UserControlModel to the MainWindowModel?;

我是否应该在 UserControl 中公开需要填充为 DependencyProperty 的属性,然后在 MainWindow 上填充它?

Should I expose the property I need to fill as DependencyProperty in my UserControl and then fill it on the MainWindow?

任何建议将不胜感激.谢谢!

Any suggestion would be appreciated. Thanks!

这就是我调用 UserControl 的方式:

Edit 1: This is how I call my UserControl:

<TabControl Grid.Row="1"
            Grid.RowSpan="2"
            Grid.Column="0"
            VerticalAlignment="Stretch">

    <!--Result View-->
    <TabItem Header="{Binding TabImportHeader}">
        <results:ResultView/>
    </TabItem>

    <!--Configuration Tab-->
    <TabItem Header="{Binding TabConfigurationHeader}">
        <configuration:ConfigurationView />
    </TabItem>
</TabControl>

出现问题的我的 UserControl 是 ResultView

My UserControl where my problem appear is the ResultView

推荐答案

MainWindowVMInstance.UserControlVMInstance.Property

UserControl 位于您的 MainWindow 内.

因此,您的 MainWindow 具有 UserControlVM 的属性(/instance).

Therefore your MainWindow has a property (/instance) of your UserControlVM.

注意:如果您在 UserControlVM 中还需要对 MainWindowVM 的引用,请将其传递给构造函数并将其存储为属性.

Note: If you also need a reference of MainWindowVM inside your UserControlVM, pass it to the constructor and store it as property.

在 xaml 中,它看起来像这样(在 MainWindow.xaml 中):

In xaml it would look like this (inside MainWindow.xaml):

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

不要忘记DataTemplate:

<DataTemplate DataType="{x:Type vm:UserControlVM}">
    <view:UserControlView/>
</DataTemplate>

<小时>

问题更新后


Edit after question update:

这是一个示例,其中包含您的部分代码,用于演示 WPF 和 MVVM 的实际操作.您只需在 UserControl.Resources 中定义一个 DataTemplate,然后通过 BindingContentControl代码>用户控制虚拟机.WPF 知道这种类型有一个 DataTemplate,并且会在 ContentControl 所在的位置添加一个 UserControlView 的实例.

This is an example with a part of your code to demonstrate WPF and MVVM in action. You simply define a DataTemplate in your UserControl.Resources and then give the ContentControl via Binding an instance of your UserControlVM. WPF knows there's a DataTemplate for this type and will add an instance of the UserControlView where the ContentControl is.

<MainWindow>
    <MainWindow.Resources>
        <DataTemplate DataType="{x:Type vm:UserControlVM}">
            <view:UserControlView/>
        </DataTemplate>
    </MainWindow.Resources>

    <!-- Your TabControl -->
    <TabControl>
        <!--Result View-->
        <TabItem Header="{Binding TabImportHeader}">
            <ContentControl Content="{Binding TabImportCONTENT}"/>
        </TabItem>
    </TabControl>
</MainWindow>

这篇关于从 MainWindow (WPF/MVVM) 访问 UserControl 中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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