C# 无法将可观察集合绑定到UserControl上的附加属性
我想将自定义类型(BoundItem)的ObservableCollection绑定到视图 我是这样使用它的:C# 无法将可观察集合绑定到UserControl上的附加属性,c#,wpf,xaml,binding,dependency-properties,C#,Wpf,Xaml,Binding,Dependency Properties,我想将自定义类型(BoundItem)的ObservableCollection绑定到视图 我是这样使用它的: <v:MyUserControlBase x:Class="My.Views.MyView" (...) h:FrameworkElementDropBehavior.MyItems="{Binding Attachments}"> 它显示了“真实” 我是这样定义依赖项属性的: public static readonly De
<v:MyUserControlBase x:Class="My.Views.MyView"
(...)
h:FrameworkElementDropBehavior.MyItems="{Binding Attachments}">
它显示了“真实”
我是这样定义依赖项属性的:
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);
}
public static readonly dependencProperty MyItemsProperty=dependencProperty.RegisterAttached(“MyItems”、typeof(ObservableCollection)、typeof(MyView)、new FrameworkPropertyMetadata(null));
公共静态字符串GetMyItems(DependencyObject元素)
{
如果(element==null)抛出新的ArgumentNullException(“MyItems”);
返回(ObservableCollection)元素.GetValue(MyItemsProperty);
}
公共静态void SetMyItems(DependencyObject元素,ObservableCollection值)
{
如果(element==null)抛出新的ArgumentNullException(“MyItems”);
元素.SetValue(MyItemsProperty,value);
}
发生的错误是:
无法在类型为“MyView”的“SetMyItems”属性上设置“Binding”。只能对DependencyObject的DependencyProperty设置“绑定”
感谢您的帮助:).x问题在于您的财产登记。它应该是
FrameworkElementDropBehavior
i,即定义属性的类,而不是所有者类型MyView
public static readonly DependencyProperty MyItemsProperty =
DependencyProperty.RegisterAttached("MyItems",
typeof(ObservableCollection<BoundItem>),
typeof(FrameworkElementDropBehavior),
new FrameworkPropertyMetadata(null));
公共静态只读从属属性MyItemsProperty=
DependencyProperty.RegisterAttached(“MyItems”,
类型(可观测采集),
类型(FrameworkElementDropBehavior),
新的FrameworkPropertyMetadata(null));
另外,SetMyItems第2个参数的类型应为ObservableCollection,GetMyItems的返回类型应为ObservableCollection。Oops,这已修复,但仍会发生错误。谢谢(:
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);
}
public static readonly DependencyProperty MyItemsProperty =
DependencyProperty.RegisterAttached("MyItems",
typeof(ObservableCollection<BoundItem>),
typeof(FrameworkElementDropBehavior),
new FrameworkPropertyMetadata(null));