カスタムタイプ(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);
}
発生するエラーは次のとおりです。
タイプ「MyView」の「SetMyItems」プロパティに「Binding」を設定することはできません。「バインディング」は、DependencyObjectのDependencyPropertyにのみ設定できます。
あなたの助けをありがとう:)。x
問題はあなたの財産登録にあります。代わりに、所有者のタイプでMyView
、それはする必要がありますFrameworkElementDropBehavior
あなたがあなたの財産を定義しているクラスをE、I。
public static readonly DependencyProperty MyItemsProperty =
DependencyProperty.RegisterAttached("MyItems",
typeof(ObservableCollection<BoundItem>),
typeof(FrameworkElementDropBehavior),
new FrameworkPropertyMetadata(null));
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加