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

C#WpfBindingUser ControlsDependency Properties

C# Problem Overview


From a custom control based on TextBox, I created a property named Items, in this way:

public class NewTextBox : TextBox
{
    public ItemCollection Items { get; set; }
}

When using the custom control in XAML, I cannot bind the property because it raises exception "A 'Binding' can only be set on a DependencyProperty of a DependencyObject.".

How do I solve this exception?

C# Solutions


Solution 1 - C#

As a side note, it is also worth noting that you will get these binding errors if you copy and paste between objects and forget to change the second typeof(Object) statement.

I couldn't figure out for a good hour why I was getting this error as everything appeared to be defined and correct. I'd moved my properties into a usercontrol as I wanted to go from a single set to a list. Thus:

public static readonly DependencyProperty FoldersProperty = DependencyProperty
    .Register("Folders", typeof(OutlookFolders), typeof(MainWindow),
    new FrameworkPropertyMetadata(new OutlookFolders()));

public OutlookFolders Folders
{
    get { return GetValue(FoldersProperty) as OutlookFolders; }
    set { SetValue(FoldersProperty, value); }
}

Should have become:

public static readonly DependencyProperty FoldersProperty = DependencyProperty
    .Register("Folders", typeof(OutlookFolders), typeof(SavedFolderControl), 
    new FrameworkPropertyMetadata(new OutlookFolders()));

public OutlookFolders Folders
{
    get { return GetValue(FoldersProperty) as OutlookFolders; }
    set { SetValue(FoldersProperty, value); }
}

Until I did this change I kept receiving the error: A 'Binding' cannot be set on the property 'Folders' of type 'SavedFolderControl'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

Solution 2 - C#

To solve this exception you need to change the property Items and add a DependencyProperty that will work as a "link" in XAML. The class will be:

public class AutocompleteTextBox : TextBox
{
    public ItemCollection Items
    {
        get {
            return (ItemCollection)GetValue(ItemsProperty); }
        set {
            SetValue(ItemsProperty, value); }
    }

    public static readonly DependencyProperty ItemsProperty =
        DependencyProperty.Register(
            "Items",
            typeof(ItemCollection),
            typeof(AutocompleteTextBox),
            new PropertyMetadata(default(ItemCollection), OnItemsPropertyChanged));

    private static void OnItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // AutocompleteTextBox source = d as AutocompleteTextBox;
        // Do something...
    }

Solution 3 - C#

Here's another gotcha: Ensure that the string in the first argument of DependencyProperty.Register() matches the name of the related property.

public static readonly DependencyProperty ItemsProperty =
    DependencyProperty.Register(
        "TheItems", // This is wrong
        typeof(ItemCollection),
        typeof(AutocompleteTextBox),
        new PropertyMetadata(default(ItemCollection), OnItemsPropertyChanged));

I ran into this issue when I renamed my property without changing the string.

Solution 4 - C#

One thing I noticed, and I am not sure it is mentioned anywhere, is that the name of your DependencyProperty must match your property name

If your property name is Items, then you DependencyProperty must be ItemsProperty

In my case, as soon as I matched them the error went away

Solution 5 - C#

Another potential cause of this is when you provide a bad type for the default value in the metadata.
For instance:

new PropertyMetadata(default(ItemCollection), OnItemsPropertyChanged)

would throw this error if you wrote instead:

new PropertyMetadata(false, OnItemsPropertyChanged)

This can also happen if you are copying and pasting from a code source.

Solution 6 - C#

I had the (runtime + designtime) message:

> An unhandled exception of type > 'System.Windows.Markup.XamlParseException' occurred in > PresentationFramework.dll > > Additional information: A 'Binding' cannot be set on the 'Property' > property of type 'Trigger'. A 'Binding' can only be set on a > DependencyProperty of a DependencyObject.

Where I was smart enough to define a Trigger on a VM property..

// incorrect.. cannot have Trigger for VM property
<Trigger Property="{Binding IsExpanded}" Value="True">
  <Setter Property="Visibility" Value="Visible"/>
</Trigger>

Which should of course be a datatrigger (which uses Binding instead of Property)

<DataTrigger Binding="{Binding IsExpanded}" Value="True">
  <Setter Property="Visibility" Value="Visible"/>
</DataTrigger>

Triggers are typically for Controls (Button, TextBox, FrameworkElement etc.) properties.

Solution 7 - C#

I had this issue due to a lack of oversight on my part. I wrote

<Button.Visibility>
   <MultiBinding Converter="{StaticResource mvbConverter}">
     <Binding Path="{Binding isActive}" />
     <Binding Path="{Binding isCashTransaction}" />
   </MultiBinding>
</Button.Visibility>

when instead i should've wrote

<Button.Visibility>
   <MultiBinding Converter="{StaticResource mvbConverter}">
     <Binding Path="isActive" />
     <Binding Path="isCashTransaction" />
   </MultiBinding>
</Button.Visibility>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmgottschildView Question on Stackoverflow
Solution 1 - C#netniVView Answer on Stackoverflow
Solution 2 - C#mgottschildView Answer on Stackoverflow
Solution 3 - C#OliverView Answer on Stackoverflow
Solution 4 - C#Pic MickaelView Answer on Stackoverflow
Solution 5 - C#Prof Von LemongargleView Answer on Stackoverflow
Solution 6 - C#EricGView Answer on Stackoverflow
Solution 7 - C#RamWillView Answer on Stackoverflow