简体   繁体   English

如何从 WPF 中的另一个 UserControl 绑定或引用 UserControl?

[英]How to bind or reference a UserControl from another UserControl in WPF?

In my application I have a UserControl , that hosts another UserControl , like this:在我的应用程序中,我有一个UserControl ,它承载另一个UserControl ,如下所示:

<UserControl x:Class="FooControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyApp">
    <local:BarControl x:Name="NestedControl" />
</UserControl>

Now within the hosted BarControl , I want to compute the position of it, relative to the hosting FooControl control.现在在托管的BarControl中,我想计算它的 position,相对于托管的FooControl控件。 Currently I am calling a member of the child control from FooControl s code behind, like so:目前我正在从FooControl的代码后面调用子控件的成员,如下所示:

this.NestedControl.ComputeRelativePos(this);

and within this method, I compute the position like this:在这种方法中,我计算 position 如下:

public void ComputeRelativePos(Control relativeTo)
{
    var pos = this.TranslatePoint(new System.Windows.Point(0, 0), relativeTo);

    // ...
}

Now I want to eliminate this call and re-compute the position from event handlers (like for example SizeChanged ) of the nested BarControl .现在我想消除这个调用并从嵌套 BarControl 的事件处理程序(例如SizeChanged )重新计算BarControl I want to do this, so that BarControl does not require to be hosted within a FooControl or another control, explicitly calling ComputeRelativePos :我想这样做,以便BarControl不需要托管在FooControl或其他控件中,显式调用ComputeRelativePos

public BarControl()
{
    this.InitializeComponent();
    this.SizeChanged += (sender, args) => 
    {
        this.ComputeRelativePos(this.OuterControl);
    };
}

However, I am not quite sure how to create a binding for the OuterControl property.但是,我不太确定如何为OuterControl属性创建绑定。 I have defined the property like so:我已经定义了这样的属性:

public Control OuterControl
{
    get { return this.GetValue(OuterControlProperty) as Control; }
    set { this.SetValue(OuterControlProperty, value); }
}

public static readonly DependencyProperty OuterControlProperty = 
    DependencyProperty.Register("OuterControl", typeof(Control), typeof(BarControl), new UIPropertyMetadata(null));

and defined the binding in the FooControl markup like this:并在FooControl标记中定义绑定,如下所示:

<local:BarControl x:Name="NestedControl" OuterControl="{Binding RelativeSource={RelativeSource Self}}" />

but the OuterControl does not appear to be set correctly:OuterControl似乎没有正确设置:

this.SizeChanged += (sender, args) => this.ComputeRelativePos(this.OuterControl); // this.OuterControl is `null`

I also tried binding to the FooControl instance name 1 :我还尝试绑定到FooControl实例名称1

<local:BarControl x:Name="NestedControl" 
    OuterControl="{Binding RelativeSource={RelativeSource Self}, Path=Name}" />

... so that I can query it from the visual tree (like suggested here ), but it did not work either. ...这样我就可以从可视化树中查询它(就像这里建议的那样),但它也不起作用。

So my general question is, how do I access a parent control, that is bound to a child's property?所以我的一般问题是,如何访问绑定到子属性的父控件? I would also accept an alternative approach to this problem, since I'm fairly inexperienced with XAML/WPF.我也会接受解决此问题的替代方法,因为我对 XAML/WPF 相当缺乏经验。

Thanks in advance.提前致谢。

1 And of course defining the DependencyProperty accordingly. 1当然,相应地定义DependencyProperty

I don't want to go into the details of what you are doing and why, but to get the dependency property working you have to change:我不想 go 详细说明您在做什么以及为什么,但是要使依赖项属性正常工作,您必须进行更改:

<local:BarControl x:Name="NestedControl" 
OuterControl="{Binding RelativeSource={RelativeSource Self}, Path=Name}" />

to:至:

<local:BarControl x:Name="NestedControl" OuterControl="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />

When this is said, your event handler size changed will be hit before the dependency control is set.当这样说时,您的事件处理程序大小更改将在设置依赖项控制之前被命中。 Dependency properties are set after the control is initialized.在控件初始化后设置依赖属性。

But afterward when you resize you will see that relativeTo is set to FooControl.但是之后,当您调整大小时,您会看到 relativeTo 设置为 FooControl。

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

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