将childcontrol的属性绑定到parentcontrol

本文关键字:parentcontrol 绑定 属性 childcontrol | 更新日期: 2023-09-27 18:16:19

我有一个UserControl(父),其中是另一个UserControl(子)。我想将Child-Control中的一个属性绑定到Parent-Control中的一个属性。这两个UserControl都是在MVVM中开发的。我现在的问题是,如何在父控件的xaml中访问子控件的属性?

我是否必须在子控件的代码后面做一些事情,或者有其他方法?

我有一个名为IPerson的接口,它看起来像:

public interface IPerson
{
  string Firstname {get;set;}
  string Lastname {get;set;}
}

父控件(PersonsView)在左侧有一个TreeView,在右侧有子控件(PersonView)。

person - view的XAML是:

<UserControl x:Class="ScM.Contents.View.PersonsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:viewModel="clr-namespace:ScM.Contents.ViewModel"
             xmlns:view="clr-namespace:ScM.Contents.View"
             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" d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.DataContext>
        <viewModel:PersonsViewModel />
    </UserControl.DataContext>
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/ScM.Interface;component/GUI/ScMStyles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <view:PersonsTreeView Grid.Column="0" Margin="2"
                             OpenPupil="{Binding DataContext.OpenPersonCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                             Persons="{Binding DataContext.Persons, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, 
            RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" />
        <GridSplitter Grid.Column="1" Width="2" ShowsPreview="True" VerticalAlignment="Stretch"
                      HorizontalAlignment="Center" Margin="0,2" />
        <view:PersonView Grid.Column="2" Margin="2"></view:PersonView>
    </Grid>
</UserControl>

Person-View仅由标签和文本框组成。PersonViewModel看起来像:

internal class PersonViewModel : ViewModelBase
{
    private readonly PersonModel _personModel;
    private IPerson _person;
    public PersonViewModel()
    {
        _personModel = new PersonModel();
    }
    public IPerson Person
    {
        get { return _person; }
        set
        {
            _person = value;
            OnPropertyChanged();
        }
    }
}

将childcontrol的属性绑定到parentcontrol

您可以使用ElementName

为父控件命名以指向它
<ParentControl x:Name=ParentControl>
   <ChildControl SomeChildControlProperty="{Binding SomeParentControlProperty,
                                            ElementName=ParentControl}"/>
</ParentControl>

或者你可以使用RelativeSource和FindAncestor:

<ParentControl>
   <ChildControl SomeChildControlProperty="{Binding SomeParentControlProperty,
          RelativeSource={RelativeSource FindAncestor,
                          AncestorType={x:Type ParentControl}}}"/>
</ParentControl>

我没有测试这段代码,所以语法可能不完全正确