简体   繁体   English

访问父USerControl ViewModel,反之亦然

[英]Accessing Parent USerControl ViewModel or vice-versa

Suppose, there is a user control named ParentUserControl in ParentView.xaml. 假设在ParentView.xaml中有一个名为ParentUserControl的用户控件。 In the code behind I have set an instance of ParentViewModel as it's DataContext. 在后面的代码中,我将ParentViewModel的实例设置为DataContext。 In ParentUserControl there's a ChildUserControl. 在ParentUserControl中有一个ChildUserControl。 How can I access ParentViewModel aka ParentUserControl's DataContext in childUserControl's view model and also in opposite way how can I access childviewmodel in parentViewModel. 我如何才能在childUserControl的视图模型中访问ParentViewModel又名ParentUserControl的DataContext,以及如何以相反的方式在parentViewModel中访问childviewmodel。

ParentView.xaml ParentView.xaml

<UserControl x:Class="Test.ParentView"             
             mc:Ignorable="d"
             Name="ParentUserControl"
             >
    <UserControl.Resources>
        <ControlTemplate x:Key="ChildControlTemplate">
            <control:ChildUSerControl/>
        </ControlTemplate>
    </UserControl.Resources>

    <Grid>
        <Border Grid.Row="0" BorderBrush="{StaticResource BorderBrush}" BorderThickness="0,0,0,0">
           <ContentControl Name="ChildControlTemplate"  Width="Auto" ></ContentControl>
        </Border>
    </Grid>
</UserControl>

ParentView.xaml.cs ParentView.xaml.cs

public ParentView()
{
    ParentViewModel parentViewModel = new ParentViewModel();
    this.DataContext = parentViewModel;
    ChildUSerControl childUserControl = new ChildUserControl();
    InitializeComponent();
    ChildControlTemplate.Content = childUserControl;
}

If you make ChildViewModel a property of ParentViewModel (as ChildVM in the example below), you can wire it up as the DataContext of ChildUserControl in the XAML. 如果将ChildViewModel设置为ParentViewModel的属性(在下面的示例中作为ChildVM),则可以将其连接为XAML中ChildUserControl的DataContext。 And while you're at it, you can create the ParentViewModel in the XAML without needing to do it in the code behind. 而且,您可以在XAML中创建ParentViewModel,而无需在后面的代码中进行操作。 Something like: 就像是:

 <UserControl x:Class="Test.ParentView"             
                 xmlns:vm="clr-namespace:NamespaceForYourParentViewModel"
                 mc:Ignorable="d"
                 Name="ParentUserControl"
                 >
       <UserControl.DataContext>
          <vm:ParentViewModel />
        </UserControl.DataContext>
        <UserControl.Resources>
            <ControlTemplate x:Key="ChildControlTemplate">
                <control:ChildUSerControl DataContext="{Binding Path=ChildVM}/>
            </ControlTemplate>
        </UserControl.Resources>

Then your parent and child view models can communicate however you like in the C# - the ParentViewModel will be able to communicate with the child as it instantiates it. 然后,您的父视图模型和子视图模型可以进行通信,但是您就像在C#中一样进行通信-ParentViewModel将能够在实例化子视图时与其进行通信。 And the ParentViewModel could be passed to the Child View Model when it's instantiated. 实例化后,ParentViewModel可以传递给子视图模型。

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

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