wpf - 为什么在父元素成功时将子元素绑定(bind)到另一个元素时绑定(bind)失败?

标签 wpf xaml binding visual-tree logical-tree

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

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

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

这是 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));

然后是 child :

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>

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


这是我的测试代码...

     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);
        }

这是测试结果...

Parent button reference is System.Windows.Controls.Button

Child button reference is null!

最佳答案

对一个长问题的简短回答是,Microsoft 不希望您在不做一点管道的情况下从 FrameworkElement 派生。

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

您可能还需要完善可视化树,并重载框架元素的排列/测量部分。 (我们在这里不这样做,因为我们在示例中不可见。)

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

A good example of someone who solved this is here

Information on overriding the logical tree is here

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

添加此函数并更改依赖属性使绑定(bind)生效。

        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);
    }

关于wpf - 为什么在父元素成功时将子元素绑定(bind)到另一个元素时绑定(bind)失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12671053/

相关文章:

用于创建自定义用户控件的 WPF 教程

javascript - 绑定(bind)子输入:value to the parent prop

wpf - Caliburn Micro Bindablecollection - 绑定(bind)到组合框项目源显示错误的文本

wpf - 如何为用户控件创建 MVVM 而不将所有内部结构暴露给消费者?

c# - Xaml 绑定(bind)全景 Windows Phone 8

c# - 使用 C#、WPF 和 DWM 保存窗口的屏幕截图

wpf - Snoop 看不到我的应用程序的子窗口

c# - 将具体实例注入(inject) Ninject 解析器

c# - XAML 中的 StrechToFit

c# - 在 c# xaml 中创建一个数学图