繁体   English   中英

ElementName绑定到DependencyObject失败

[英]ElementName Binding to a DependencyObject fails

我实现了一个非常简单的DependencyObject(直接从DependencyObject派生),其中一个DependencyProperty 的类型为double:

public class SimpleDepObj : DependencyObject 
{
    public static DependencyProperty ValueProperty;

    static SimpleDepObj()
    {
        var pmValue = new PropertyMetadata(3.14);
        ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(SimpleDepObj), pmValue);
    }

    public double Value
    {
        get { return (double) GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }
}

另外,我制作了此类的副本,但派生自FrameworkElement而不是DependencyObject:

public class SimpleFrmWrkElem : FrameworkElement
{
    public static DependencyProperty ValueProperty;

    static SimpleFrmWrkElem()
    {
        var pmValue = new PropertyMetadata(3.14);
        ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(SimpleFrmWrkElem), pmValue);
    }

    public double Value
    {
        get { return (double)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }
}

为了测试此类的数据绑定,我编写了以下XAML代码:

<Window ...> 
    <Window.Resources>
         <sys:Double x:Key="inputValue">4711</sys:Double>
    </Window.Resources>
    <StackPanel>
         <TextBox Name="tbxSource" Text="4712" Visibility="Hidden"/>

         <!-- First test -->
         <xamlBinding:SimpleDepObj x:Name="simpleDepObj1" Value="{Binding Source={StaticResource inputValue}}"/>
         <TextBlock Text="{Binding ElementName=simpleDepObj1, Path=Value }" Background="LightCoral"/>

         <!-- Second test -->
         <xamlBinding:SimpleDepObj x:Name="simpleDepObj2" Value="{Binding ElementName=tbxSource, Path=Text}"/>
         <TextBlock Text="{Binding ElementName=simpleDepObj2, Path=Value }" Background="LightGreen"/>

         <!-- Third test -->
         <xamlBinding:SimpleFrmWrkElem x:Name="simpleFrmWrkElem" Value="{Binding ElementName=tbxSource, Path=Text}"/>
         <TextBlock Text="{Binding ElementName=simpleFrmWrkElem, Path=Value }" Background="LightBlue"/>

    </StackPanel>
</Window>

在窗口类的资源中,有一个类型为double的StaticResource inputValue,值为4711。在StackPanel的开头,有一个文本框为“ 4712”的文本。 这两个元素是我测试的输入。

在第一个测试中,我将StaticResource inputValue绑定到SimpleDepObj的Value-Property。为显示Value-Property,我添加了一个Textblock。 SimpleDepObj的Value-Property绑定到此TextBlock的Text-Property。 该绑定有效,并显示期望值4711。

在第二个测试中,我尝试将TextBox tbxSource的Text-Property绑定到另一个SimpleDepObj。 不幸的是,绑定失败并显示以下消息: System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:Path = Text; DataItem = null; 目标元素是'SimpleDepObj'(HashCode = 11958757); 目标属性为“值”(类型为“双精度”)

第三个测试文件与第二个测试文件几乎相同,除了现在的绑定目标文件类型为SimpleFrmWrkElem,它是从FrameworkElement派生的。 与以DependencyObject作为绑定目标的第二个测试相反,此绑定不会失败。

现在我的问题是:为什么第二项测试失败? 消息内容是什么? 找不到目标元素的管理FrameworkElement或FrameworkContentElement。 意思? 看起来绑定引擎无法找到textBox tbxSource(DataItem = null),但是为什么呢? 第三个测试完全相同,只是目标元素是从FrameworkElement而不是DependencyObject派生的。 源元素与第二个测试完全相同。

非常感谢您提前回答!

暂无
暂无

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

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