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

Jer*_*ite 2 wpf xaml binding visual-tree logical-tree

假设我有两个可以引用第三个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));
Run Code Online (Sandbox Code Playgroud)

然后孩子:

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));
}
Run Code Online (Sandbox Code Playgroud)

好 -

现在说我将它们添加到我的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>
Run Code Online (Sandbox Code Playgroud)

当我使用EXACT相同的表示法时,为什么绑定在父级上工作但在子级上失败?


这是我的测试代码......

     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);
        }
Run Code Online (Sandbox Code Playgroud)

这是测试结果......

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

子按钮引用为空!

Jer*_*ite 5

对一个长问题的简短回答是,微软不希望你在没有做一点管道的情况下从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);
    }
Run Code Online (Sandbox Code Playgroud)