Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

designer shows A 'Binding' can only be set on a DependencyProperty of a DependencyObject error

Tags:

c#

wpf

I've created a user control that has a DependencyProperty, and when I try binding to it I get an error in the designer:

" 'Binding' cannot be set on the 'ROCValue' property of type 'RocIndicator'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

Edit: I've added the static modifier, but I still get that error. and yes, I've restarted visual studio.

public partial class RocIndicator : UserControl
{
    public static readonly DependencyProperty ROCValueProperty =
        DependencyProperty.Register("ROCValue", typeof(double), typeof(RocIndicator),
        new FrameworkPropertyMetadata(0.0, new PropertyChangedCallback(ValueChanged)));


    public double ROCValue
    {
        get { return (double)GetValue(ROCValueProperty); }
        set { SetValue(ROCValueProperty, value); }
    }
}

this is the XAML:

<View:RocIndicator ROCValue="{Binding ROC}" Margin="0,10,0,0" HorizontalAlignment="Center" Width="35"/>

but when I build & run it works. why is this error showing?

like image 722
Nataly87 Avatar asked Aug 13 '14 12:08

Nataly87


2 Answers

Dependency property declaration must be static:

public static readonly DependencyProperty ROCValueProperty ...
like image 108
dkozl Avatar answered Oct 13 '22 12:10

dkozl


I had the same issue once when copying a UserControl from one project to another. I tried closing the solution, restarting Visual Studio and even restarting my PC. None of those worked.

I then unloaded and reloaded the project and this seemed to fix it. (Potentially a Visual Studio build bug).

To unload the project, right click on your project in Solution Explorer and click 'Unload Project'. Then right click again and click 'Reload Project'.

like image 42
Shakeel Choudhury Avatar answered Oct 13 '22 14:10

Shakeel Choudhury