2

カスタムの添付動作を作成しました:

public static class CustomItemsBehaviour
{
    public static readonly DependencyProperty MyTestProperty =
     DependencyProperty.RegisterAttached(
         "MyTest",
         typeof(string),
         typeof(ItemsControl),
         new UIPropertyMetadata(""));

     public static string GetMyTest(ItemsControl itemsControl)
     {
        return (string)itemsControl.GetValue(MyTestProperty);
     }

     public static void SetMyTest(ItemsControl itemsControl, string value)
     {
        itemsControl.SetValue(MyTestProperty, value);
     }
}


私はそれを次のように使用しようとしています:

<ListBox
    ItemsSource="{Binding Path=Items}" 
    AttachedBehaviours:CustomItemsBehaviour.MyTest="{Binding TestValue}">


しかし、それは失敗します:

{"A 'Binding' cannot be set on the 'SetMyTest' property of type 'ListBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject."}


ビュー モデルの値を MyTest の値にバインドしたいと考えています。これは可能ですか?

4

2 に答える 2

5

問題は登録コードにあります。typeof(CustomItemsBehaviour)所有者タイプとして渡す必要があります。

public static readonly DependencyProperty MyTestProperty =
 DependencyProperty.RegisterAttached(
     "MyTest",
     typeof(string),
     typeof(CustomItemsBehaviour),
     new UIPropertyMetadata(""));
于 2013-06-27T14:19:48.200 に答える