繁体   English   中英

将子控件的属性绑定到父控件

[英]Bind property of childcontrol to parentcontrol

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

我是否必须在子控件的代码背后做些事情,还是有另一种方法?

我有一个名为IPerson的界面,看起来像:

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

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

人员视图的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>

人员视图仅由标签和文本框组成。 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();
        }
    }
}

您可以使用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>

我没有测试此代码,因此语法可能不完全正确

暂无
暂无

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

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