Taedrin
Taedrin

Reputation: 535

Can't bind to my dependency property

I have a dependency property in a class which inherits from Canvas, like so:

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

I'm trying to bind to it in Xaml like so:

<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 />

For completeness, the properties I'm trying to bind from:

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

However, when I try to run/debug the project, I get an exception on 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.

No matter how many times I look at examples, it seems to me that InnerHue should be a valid dependency property. I also double checked to make sure that Canvas is a DependencyObject (if it weren't, GetValue and SetValue should throw a compiler error). What in the world am I doing incorrectly? As I am relatively new to WPF, I can't help but feel that I'm missing something obvious.

Upvotes: 1

Views: 1361

Answers (1)

eran otzap
eran otzap

Reputation: 12533

You gave the wrong owner type for your DependencyProperty

you wrote LuminanceVisualizer and it should be HueVisualizer .

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

Upvotes: 2

Related Questions