gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:05:13 26 4
gpt4 key购买 nike

我目前正在创建一个应用程序来测试 WPF 的灵 active 。我有一些用户控件,当涉及到它的 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 的 UserControl SupplierView。

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 SupplierView 绑定(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com