c# - 在用户控件内设置用户控件的数据上下文,无论用户控件有多少层

标签 c# wpf mvvm user-controls mvvm-light

我目前正在创建一个应用程序来测试 WPF 的灵活性。我有一些用户控件,当涉及到其 ViewModel 时,它们的目的是非常透明的。我所说的透明是指用户控件可以使用任何类型的 ViewModel,前提是 ViewModel 具有绑定(bind)到该用户控件内的控件所需的所有属性。我通过将 ViewModel 分配为特定用户控件的数据上下文来实现此目的。

当只有两个用户控件时(一个可以访问 ViewModelLocator,一个需要前者的数据上下文声明),此方法有效。当用户控件达到 3 层或更多时,我不知道该怎么办。有没有办法在驻留在有权访问 ViewModelLocator 的用户控件中的用户控件中设置用户控件的数据上下文?

下面是一些可以澄清我的问题的代码。

此代码是父用户控件。它旨在用于利用 MAF 的应用程序。我使用非静态 ViewModelLocator 来确保每个插件实例使用不同的 ViewModelLocator 实例,并且因为该插件没有自己的 app.xaml (因此没有全局资源)。正如您所看到的,我将一个来自单独程序集的用户控件放置在网格中,然后声明其数据上下文,以便所述用户控件能够与父用户控件的 ViewModelLocator 进行交互。

<UserControl x:Class="TestApp.Inventory.Common.Views.MaterialsNewView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:vm="clr-namespace:TestApp.Inventory.Common.ViewModel"
             xmlns:views="clr-namespace:TestApp.Inventory.Common.Views"
             xmlns:viewsSupp="clr-namespace:TestApp.Supplier.Common.Views;assembly=TestApp.Supplier.Common"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="607" Width="616" Loaded="UserControl_Loaded">

    <UserControl.Resources>
        <vm:ViewModelLocator x:Key="Locator" />
    </UserControl.Resources>

    <UserControl.DataContext>
        <Binding Path="MaterialsNewView" Source="{StaticResource Locator}" />
    </UserControl.DataContext>

    <Grid>
        <views:SupplierView x:Name="supplierView" Margin="145,306,0,0" HorizontalAlignment="Left" Width="328" Height="258" VerticalAlignment="Top" DataContext="{Binding Source={StaticResource Locator}, Path=SupplierView}" />
    </Grid>
</UserControl>

然后我就有了子用户控件的代码。就像我之前所说的那样,子用户控件的目的是在 ViewModel 方面是透明的。这就是为什么每次都应该在父用户控件中声明数据上下文。

<UserControl x:Class="TestApp.Supplier.Common.Views.SupplierView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ignore="http://www.ignore.com"
        mc:Ignorable="d ignore" Height="289" Width="352" 
        xmlns:my="clr-namespace:TestApp.Lookup.Common.Views;assembly=TestApp.Lookup.Common">

    <Grid>
        <my:MaterialTypeListView Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Grid, AncestorLevel=1}, Path=ActualHeight}" HorizontalAlignment="Left" Name="materialTypeListView1" VerticalAlignment="Top" Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Grid, AncestorLevel=1}, Path=ActualWidth}" />
    </Grid>
</UserControl>

我的问题是当子用户控件有自己的子用户控件时。我不知道如何从父用户控件声明其数据上下文。目标是无论有多少层用户控件,它们都应该与父用户控件的 ViewModelLocator 交互。

最佳答案

DependencyProperty添加到名为MaterialTypeList的UserControlSupplierView。

public partial class SupplierView
{
    public List<string> MaterialTypeList
    {
        get { return (List<string>)GetValue(MaterialTypeListProperty); }
        set { SetValue(MaterialTypeListProperty, value);}
    }

    public static readonly DependencyProperty MaterialTypeListProperty =
            DependencyProperty.Register("MaterialTypeList", typeof(string), typeof(SupplierView),
            new PropertyMetadata(null));
}

将SupplierView上的UserControl MaterialNewView的绑定(bind)添加到MaterialTypeList

 <UserControl x:Class="TestApp.Supplier.Common.Views.SupplierView"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:ignore="http://www.ignore.com"
            mc:Ignorable="d ignore" Height="289" Width="352" 
            xmlns:my="clr-namespace:TestApp.Lookup.Common.Views;assembly=TestApp.Lookup.Common">

        <Grid>
            <my:MaterialTypeListView 
DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl, AncestorLevel=1}, Path=MaterialTypeList}"
Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Grid, AncestorLevel=1}, Path=ActualHeight}" HorizontalAlignment="Left" Name="materialTypeListView1" VerticalAlignment="Top" Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Grid, AncestorLevel=1}, Path=ActualWidth}" />
        </Grid>
    </UserControl>

将 MaterialTypeListView 上的 UserControl SupplyView 的绑定(bind)添加到 MaterialTypeList。

    <UserControl x:Class="TestApp.Inventory.Common.Views.MaterialsNewView"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:vm="clr-namespace:TestApp.Inventory.Common.ViewModel"
                 xmlns:views="clr-namespace:TestApp.Inventory.Common.Views"
                 xmlns:viewsSupp="clr-namespace:TestApp.Supplier.Common.Views;assembly=TestApp.Supplier.Common"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" Height="607" Width="616" Loaded="UserControl_Loaded">

        <UserControl.Resources>
            <vm:ViewModelLocator x:Key="Locator" />
        </UserControl.Resources>

        <UserControl.DataContext>
            <Binding Path="MaterialsNewView" Source="{StaticResource Locator}" />
        </UserControl.DataContext>

        <Grid>
            <views:SupplierView x:Name="supplierView" Margin="145,306,0,0" 
MaterialTypeList="{Binding [HERE YOUR LIST OF MATERIALTYPE]}"
HorizontalAlignment="Left" Width="328" Height="258" VerticalAlignment="Top" DataContext="{Binding Source={StaticResource Locator}, Path=SupplierView}" />
        </Grid>
    </UserControl>

关于c# - 在用户控件内设置用户控件的数据上下文,无论用户控件有多少层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16148442/

相关文章:

c# - WPF 通过约束绑定(bind)到集合

c# - 如何在处理函数的其余部分之前更新 WPF 中的 UI 元素?

wpf - 使用XAML或MVVM将焦点设置为UI元素

c# - MVVM - ListView ItemClick 如何使用 Bindig 执行 RelayCommand

c# - 更新 "live"WPF Canvas (折线),当 PointCollection 发生变化时

c# - asp.net Oracle 调整线程休眠

c# - 返回多个文件夹的文件列表的最有效方法

c# - MVC 映射 - RedirectToAction 不起作用

c# - LINQ Group by 在多个表上使用嵌套分组依据以及连接和聚合

wpf - 如何在 ControlTemplate 触发器中使用自定义属性