Nataly87
Nataly87

Reputation: 243

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

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?

Upvotes: 6

Views: 8628

Answers (3)

Alex Parloti
Alex Parloti

Reputation: 733

For each xaml form, *.g.cs and .g.i.cs files are automatically created in the /objs/Debug// folder. Sometimes some of these files are not actualized by the "Rebuild Solution" option.

I could not yet identify when this happens.

But I solved it by deleting the "obj" folder in the project directory and rebuilding the solution.

Upvotes: 0

Shakeel Choudhury
Shakeel Choudhury

Reputation: 21

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'.

Upvotes: 1

dkozl
dkozl

Reputation: 33384

Dependency property declaration must be static:

public static readonly DependencyProperty ROCValueProperty ...

Upvotes: 10

Related Questions