当父元素成功时,将子元素绑定到另一个元素时,为什么绑定失败



假设我有两个类可以引用第三个UI对象(在本例中为按钮)。

此外,父类可以包含子类的元素。

如果它们都以相同的方式绑定到同一控件,则子项将失败,但父项成功。

这是 WPF 中的错误吗?

<小时 />

父级 :

class MyFrameworkElement : FrameworkElement
{
    // A depenedency property that will contain a child element sub-element
    private static readonly DependencyProperty ChildElementProperty =
                    DependencyProperty.Register("ChildElement",
                    typeof(MyChildElement),
                    typeof(MyFrameworkElement),
                    new PropertyMetadata());
    [Category("ChildProperties")]
    public MyChildElement ChildElement
    {
        set { SetValue(ChildElementProperty, value); }
        get { return (MyChildElement)GetValue(ChildElementProperty); }
    }

    // Now, a reference to some other control, in this case we will bind a button to it!
    public UIElement ButtonReferenceInParent
    {
        get { return (UIElement)GetValue(ButtonReferenceInParentProperty); }
        set { SetValue(ButtonReferenceInParentProperty, value); }
    }
    // Using a DependencyProperty as the backing store for ButtonReferenceInParent.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ButtonReferenceInParentProperty =
        DependencyProperty.Register("ButtonReferenceInParent", typeof(UIElement), typeof(MyFrameworkElement), new UIPropertyMetadata(null));
<小时 />

然后孩子:

public class MyChildElement : FrameworkElement
{
    public UIElement ButtonReferenceInChild
    {
        get { return (UIElement)GetValue(ButtonReferenceInChildProperty); }
        set { SetValue(ButtonReferenceInChildProperty, value); }
    }
    public static readonly DependencyProperty ButtonReferenceInChildProperty =
        DependencyProperty.Register("ButtonReferenceInChild", typeof(UIElement), typeof(MyChildElement), new UIPropertyMetadata(null));
}

还行-

现在假设我像这样将它们添加到我的 XAML 中:

<Grid>
    <my:MyFrameworkElement x:Name="ParentName" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ButtonReferenceInParent="{Binding ElementName=buttonisme}">
        <my:MyFrameworkElement.ChildElement>
            <my:MyChildElement x:Name="ChildName" ButtonReferenceInChild="{Binding ElementName=buttonisme}"/>
        </my:MyFrameworkElement.ChildElement>
    </my:MyFrameworkElement>
    
    <Button x:Name="buttonisme" Click="buttonisme_Click" />
</Grid>

当我使用完全相同的表示法时,为什么绑定在父项上有效,但在子项上失败?

<小时 />

这是我的测试代码...

     Console.WriteLine("Parent button reference is {0}", ParentName.ButtonReferenceInParent);
        if (ChildName.ButtonReferenceInChild == null)
        {
            Console.WriteLine("Child button reference is null!");
        } 
        else
        {
            Console.WriteLine("Child button is {0}", ChildName.ButtonReferenceInChild);
        }

这是测试结果...

父按钮引用是 System.Windows.Controls.Button

子按钮引用为空!

一个长问题的简短回答是,Microsoft不希望你不做一点管道就从 FrameworkElement 派生出来。

只是做派生,打破了按元素名称进行绑定时使用的逻辑树。

您可能还必须填充可视化树,并重载框架元素的排列/度量部分。 (我们在这里不这样做,因为我们在示例中不是视觉的。

在这种特定情况下,我们需要将对象的任何子元素添加到逻辑树中,或者破坏绑定子元素的能力。

解决这个问题的人的一个很好的例子在这里

有关覆盖逻辑树的信息在这里

无论如何,修复此 SIMPLE 示例所需的代码仅依赖于逻辑树(因为子对象并不是真正可视化的)。

添加此函数并更改依赖项属性可使绑定工作。

        private static readonly DependencyProperty ChildElementProperty =
                    DependencyProperty.Register("ChildElement",
                    typeof(MyChildElement),
                    typeof(MyFrameworkElement),
                    new PropertyMetadata(OnChildElementChanged));
    private static void OnChildElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MyFrameworkElement control = d as MyFrameworkElement;
        if (e.OldValue != null)
        {
            control.RemoveLogicalChild(e.OldValue);
        }
        control.AddLogicalChild(e.NewValue);
    }

首先,当你像这样设置 xaml 时:

<my:MyFrameworkElement x:Name="ParentName" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ButtonReferenceInParent="{Binding ElementName=buttonisme}"/>
<my:MyChildElement x:Name="ChildName" ButtonReferenceInChild="{Binding ElementName=buttonisme}"/>

它有效。我这样做是因为我怀疑可视化树向上遍历搜索您在绑定中使用的元素名称。

我仍在弄清楚如何在您的嵌套方案中成功绑定。但也许这可能会给你一些提示...

最新更新