-2

I've created a custom control in a separate project and it worked correctly and I was able to bind to it from the main window.. I've copied it over to my working project and now I'm getting an error when I try to bind to it in the xaml designer.

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

<controls:DTControl Grid.Row="1" CurrentDateTime="{Binding SCI.CheckInTime}"/>

My DependencyProperty is declared in my control's code behind.

public static readonly DependencyProperty SetCurrentDateTimeProperty =
    DependencyProperty.Register("CurrentDateTime", 
        typeof(DateTime), typeof(DTControl), 
        new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnDateChanged)));

public DateTime CurrentDateTime {
    get {
        return (DateTime)GetValue(SetCurrentDateTimeProperty);
    }
    set {
        SetValue(SetCurrentDateTimeProperty, value);
    }
}

What's causing the designer to not recognize the DP?

bwoogie
  • 4,339
  • 12
  • 39
  • 72

1 Answers1

1

Naming convention is important. You should not have "Set" in the dependency property. If your property name is "Hello", then dependency property should be "HelloProperty" and not "XxxHelloProperty". Even though it works during run time, intellisense will not recognize this.

You should also prefer to use nameof instead of a string literal for the property name.

public DateTime CurrentDateTime
{
    get { return (DateTime)GetValue(CurrentDateTimeProperty); }
    set { SetValue(CurrentDateTimeProperty, value); }
}

public static readonly DependencyProperty CurrentDateTimeProperty =
    DependencyProperty.Register(
        nameof(CurrentDateTime),
        typeof(DateTime),
        typeof(DTControl),
        new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnDateChanged)));

Quick tip: If you are using visual studio, then try the snippet "propdp" to generate it automatically for you.

Clemens
  • 123,504
  • 12
  • 155
  • 268
Lingam
  • 609
  • 5
  • 16