【问题标题】:Can't bind to my dependency property无法绑定到我的依赖属性
【发布时间】:2013-11-21 20:11:08
【问题描述】:

我在一个继承自 Canvas 的类中有一个依赖属性,如下所示:

public partial class HueVisualizer : Canvas
{
    public HueVisualizer()
    {
        InitializeComponent();
    }

    public decimal InnerHue
    {
        get { return (decimal)GetValue(HueProperty); }
        set { SetValue(HueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for InnerHue,Saturation and Luminance.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HueProperty =
        DependencyProperty.Register("InnerHue", typeof(decimal), typeof(LuminanceVisualizer), new FrameworkPropertyMetadata((decimal)0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
}

我正在尝试像这样在 Xaml 中绑定它:

<UserControl x:Class="Project1.UserControl1"
         x:Name="TheControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:project1="clr-namespace:Project1"
         mc:Ignorable="d"
         d:DesignHeight="120" d:DesignWidth="300">
...
    <Grid Grid.Row="0" x:Name="HueGrid">
        <project1:HueVisualizer x:Name="HueVisual" 
                                InnerHue ="{Binding ElementName=TheControl, Path=Hue, Mode=TwoWay}"
                                Height="20"
                                Width="{Binding ElementName=TheControl, Path=Width}"/>
    </Grid>
<UserControl />

为了完整起见,我尝试绑定的属性来自:

    public partial class UserControl1 : UserControl, INotifyPropertyChanged
{


    public decimal Hue
    {
        get { return (decimal)GetValue(HueProperty); }
        set { SetValue(HueProperty, value); }
    }

    ...

    // Using a DependencyProperty as the backing store for Hue.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HueProperty =
        DependencyProperty.Register("Hue", typeof(decimal), typeof(UserControl1), new 
        FrameworkPropertyMetadata((decimal)0));
...
}

但是,当我尝试运行/调试项目时,InitializeComponent() of UserControl1 出现异常:

A 'Binding' cannot be set on the 'InnerHue' property of type 'HueVisualizer'. A
'Binding' can only be set on a DependencyProperty of a DependencyObject.

无论我看多少次示例,在我看来 InnerHue 应该是一个有效的依赖属性。我还仔细检查以确保 Canvas 是一个 DependencyObject(如果不是,GetValue 和 SetValue 应该会引发编译器错误)。我到底做错了什么?由于我对 WPF 比较陌生,所以我不禁觉得我遗漏了一些明显的东西。

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    您为 DependencyProperty 提供了错误的所有者类型

    你写了 LuminanceVisualizer,它应该是 HueVisualizer 。

       public static readonly DependencyProperty HueProperty =
        DependencyProperty.Register("InnerHue", typeof(decimal), 
                          typeof(LuminanceVisualizer), // Replace with HueVisualizer 
                         new FrameworkPropertyMetadata((decimal)0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault))
    

    【讨论】:

    • 尽管有四双眼睛盯着代码,但我们都没有发现错字。感谢您的快速回复!
    • 这是一个非常常见的错误,我曾经有一个 sn-p 阻止了这种情况..你也可以在你的初始值中写 0.0
    • 你确定吗?我以为编译器将 0.0 视为双精度/浮点数,与十进制不兼容。
    • 对不起,我以为是双重的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 2016-03-10
    • 2016-01-22
    • 2023-03-26
    • 2018-05-21
    • 1970-01-01
    • 2011-12-26
    相关资源
    最近更新 更多