私はコントロールライブラリを作成しています。このライブラリには、ユーザーの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で定義されたいくつかの他の添付プロパティにバインド」
私が行方不明です?バインディングではなく、への結合の場合は正常に動作するようです』。
事前に感謝します。
こんにちは、MyLibCommonPropertiesはDependecyObject –
だけの推測から派生しなければなりません、しかし、DependencyObjectとあなたの取得/のsettitle最初のパラメータはありません変更することができたUIElement 。また、Attacheプロパティを登録する際に、3番目のパラメータは、必要なターゲットではなく、添付プロパティの所有者でなければなりません。 MyLibCommonPropertiesに変更します。 – dowhilefor
'HorizontalShelf'とは何ですか?代わりに 'StackPanel'やそれに類する組み込みコントロールで試しましたか?他のすべてはうまくいくようです。私は 'HorizontalShelf'が子を論理的な子として認識しないカスタムコントロールだと仮定することができます。こちらをご覧ください:http://kentb.blogspot.com/2008/10/customizing-logical-children.html –