Unable to bind observable collection to attached property in UserControl

I want to bind a custom type of ObservableCollection (BoundItem) to a view.

I use it as follows:

<v:MyUserControlBase x:Class="My.Views.MyView"
         (...)
         h:FrameworkElementDropBehavior.MyItems="{Binding Attachments}">

Attachments are seen in the ViewModel as:

public ObservableCollection<BoundItem> Attachments 
{ 
   get { return _Attachments; } 
   set { _Attachments = value; } 
}

My view is the actual DependencyObject, because when I do the following piece of code in the code behind the view:

MessageBox.Show((this as DependencyObject).ToString());

True is displayed.

I defined the Dependency property as follows:

    public static readonly DependencyProperty MyItemsProperty = DependencyProperty.RegisterAttached("MyItems", typeof(ObservableCollection<BoundItem>), typeof(MyView), new FrameworkPropertyMetadata(null));
    public static string GetMyItems(DependencyObject element)
    {
        if (element == null) throw new ArgumentNullException("MyItems");
        return (ObservableCollection<BoundItem>)element.GetValue(MyItemsProperty);
    }
    public static void SetMyItems(DependencyObject element, ObservableCollection<BoundItem> value)
    {
        if (element == null) throw new ArgumentNullException("MyItems");
        element.SetValue(MyItemsProperty, value);
    }

An error has occurred:

Binding cannot be set in the SetMyItems property of type MyView. Binding can only be set in the DependencyProperty of a DependencyObject.

Thanks for the help :). X

+4
2

. MyView FrameworkElementDropBehavior i, e , .

public static readonly DependencyProperty MyItemsProperty =
     DependencyProperty.RegisterAttached("MyItems", 
                                        typeof(ObservableCollection<BoundItem>), 
                                        typeof(FrameworkElementDropBehavior), 
                                        new FrameworkPropertyMetadata(null));
+2

, SetMyItems ObservableCollection, GetMyItems ObservableCollection.

0

All Articles