我希望将自定义类型(BoundItem)的ObservableCollection绑定到视图上。
我是这样使用它的:
<v:MyUserControlBase x:Class="My.Views.MyView"
(...)
h:FrameworkElementDropBehavior.MyItems="{Binding Attachments}">
在ViewModel中,"附件"被定义为:
public ObservableCollection<BoundItem> Attachments
{
get { return _Attachments; }
set { _Attachments = value; }
}
我的视图是一个实际的DependencyObject,因为当我在视图的后台代码中执行以下代码:
MessageBox.Show((this as DependencyObject).ToString());
它显示为“True”。
我这样定义了我的依赖属性:
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);
}
发生的错误是:
谢谢您的帮助 :) .x'Binding' 无法设置为类型为 'MyView' 的 'SetMyItems' 属性。'Binding' 只能设置为 DependencyObject 的 DependencyProperty。