コンテキスト: オブジェクトに動作をアタッチするためのシステムを実装しています*。コードでは意図したとおりに機能しますが、XAML では機能しません。
※はい、承知しておりますMicrosoft.Expression.Interactivity.dll
要するに、私はこれができるようになりたい..
<Rectangle>
<m:BehaviorExtensions.Behavior>
<m:HoverBehavior />
</m:BehaviorExtensions.Behavior>
</Rectangle>
..しかし、これしかできないようです:
<Window.Resources>
<m:HoverBehavior x:Key="Hover" />
</Window.Resources>
<Rectangle m:BehaviorExtensions.Behavior="{DynamicResource Hover}" />
m:BehaviorExtensions
上記の希望に満ちた例のように、ネストされたタグでマークアップしようとしても、Intellisense に表示されません。ここで何が欠けていますか?
これは、添付プロパティを定義するコードです。
public static class BehaviorExtensions
{
public static readonly DependencyProperty BehaviorProperty = DependencyProperty.RegisterAttached("Behavior", typeof(IBehavior), typeof(BehaviorExtensions), new FrameworkPropertyMetadata(null, BehaviorChangedCallback));
public static void SetBehavior(UIElement element, IBehavior behavior)
{
if (element == null) { throw new ArgumentNullException("element"); }
element.SetValue(BehaviorProperty, behavior);
}
public static IBehavior GetBehavior(UIElement element)
{
if (element == null) { throw new ArgumentNullException("element"); }
return (IBehavior)element.GetValue(BehaviorProperty);
}
private static void BehaviorChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
IBehavior behavior;
if (e.OldValue != null)
{
behavior = (IBehavior)e.OldValue;
behavior.DetachFrom((UIElement)d);
}
if (e.NewValue == null) return;
behavior = (IBehavior)e.NewValue;
behavior.AttachTo((UIElement)d);
}
}
編集 - 教訓:
これで問題なく動作することがわかりました。プログラムを (文字通り) 取得する必要があるのは Intellisense です。私がそれを完全に無視して、必要なものだけを入力したとき、物事はうまくいきました-つまり、問題なくコンパイルされました。XAML に関して言えば、Intellisense をあまり尊重しないことが医師の指示であるようです。