2011-11-11 6 views
9

私はコントロールライブラリを作成しています。このライブラリには、ユーザーのUIElementsが設定されたカスタムパネルがいくつかあります。私はこれ書いた場合、添付プロパティを別の依存関係プロパティにバインドできません

// Attached properties common to every UIElement 
public static class MyLibCommonProperties 
{ 
    public static readonly DependencyProperty TitleProperty = 
     DependencyProperty.RegisterAttached( 
      "Title", 
      typeof(String), 
      typeof(UIElement), 
      new FrameworkPropertyMetadata(
       "NoTitle", new PropertyChangedCallback(OnTitleChanged)) 
      ); 

    public static string GetTitle(UIElement _target) 
    { 
     return (string)_target.GetValue(TitleProperty); 
    } 

    public static void SetTitle(UIElement _target, string _value) 
    { 
     _target.SetValue(TitleProperty, _value); 
    } 

    private static void OnTitleChanged(DependencyObject _d, DependencyPropertyChangedEventArgs _e) 
    { 
     ... 
    } 
} 

その後:私のlib内のすべての子要素は「タイトル」プロパティを持っている必要がありますので、私は次のように書いた

<dl:HorizontalShelf> 
    <Label dl:MyLibCommonProperties.Title="CustomTitle">1</Label> 
    <Label>1</Label> 
    <Label>2</Label> 
    <Label>3</Label> 
</dl:HorizontalShelf> 

すべてが正常に動作し、プロパティを指定します値が、私はこのようにいくつかの他のUIElementたDependencyPropertyにそのプロパティをバインドしようとした場合:

<dl:HorizontalShelf> 
    <Label dl:MyLibCommonProperties.Title="{Binding ElementName=NamedLabel, Path=Name}">1</Label> 
    <Label>1</Label> 
    <Label>2</Label> 
    <Label Name="NamedLabel">3</Label> 
</dl:HorizontalShelf> 

例外がスローされます:「A 『のsettitle』プロパティに設定することができません 『バインド』タイプ 'ラベル'の。 DependencyObjectのDependencyPropertyにのみ設定することができ「バインディング」。名前 『私はMyLibCommonPropertiesで定義されたいくつかの他の添付プロパティにバインド」

私が行方不明です?バインディングではなく、への結合の場合は正常に動作するようです』。

事前に感謝します。

+0

こんにちは、MyLibCommonPropertiesはDependecyObject –

+1

だけの推測から派生しなければなりません、しかし、DependencyObjectとあなたの取得/のsettitle最初のパラメータはありません変更することができたUIElement 。また、Attacheプロパティを登録する際に、3番目のパラメータは、必要なターゲットではなく、添付プロパティの所有者でなければなりません。 MyLibCommonPropertiesに変更します。 – dowhilefor

+0

'Horizo​​ntalShelf'とは何ですか?代わりに 'StackPanel'やそれに類する組み込みコントロールで試しましたか?他のすべてはうまくいくようです。私は 'Horizo​​ntalShelf'が子を論理的な子として認識しないカスタムコントロールだと仮定することができます。こちらをご覧ください:http://kentb.blogspot.com/2008/10/customizing-logical-children.html –

答えて

13

私はバインディングが暗黙のうちに親クラスのスペックを使用していますので、それがかもしれないと思うMyLibCommonProperties

public static readonly DependencyProperty TitleProperty = 
    DependencyProperty.RegisterAttached( 
     "Title", 
     typeof(String), 
     typeof(MyLibCommonProperties), // Change this line 
     new FrameworkPropertyMetadata(
      "NoTitle", new PropertyChangedCallback(OnTitleChanged)) 
     ); 

にあなたのDependencyProperty定義でUIElementを交換してください電話番号​​にの代わりにLabel.SetTitle()を呼び出しています

私はいくつかのカスタムTextBoxプロパティで同じ問題がありました。私はtypeof(TextBox)を使用した場合、私は値にバインドすることができませんでしたが、私は、その後typeof(TextBoxHelpers)を使用した場合、私は

+1

+1素晴らしい!ありがとう – Trap

+0

も私のために働く。ありがとう – IFink

関連する問題