Xamlでは、次のようなテキストボックスのカスタマイズされた動作を配置できます。
<TextBox>
<i:Interaction.Behaviors>
<My:TextBoxNewBehavior/>
</i:Interaction.Behaviors>
</TextBox>
すべてのTextBoxにこの動作を持たせたいので、この動作を暗黙のスタイルにするにはどうすればよいですか?
<Style TargetType="TextBox">
<Setter Property="BorderThickness" Value="1"/>
....
</Style>
更新:情報をありがとう。以下に提案されている方法を試してみてください。アプリがクラッシュします。
<Setter Property="i:Interaction.Behaviors">
<Setter.Value>
<My:TextBoxNewBehavior/>
</Setter.Value>
</Setter>
私の行動は次のようなものです。
public class TextBoxMyBehavior : Behavior<TextBox>
{
public TextBoxMyBehavior()
{
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.KeyUp += new System.Windows.Input.KeyEventHandler(AssociatedObject_KeyUp);
}
void AssociatedObject_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//....
}
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.KeyUp -= new System.Windows.Input.KeyEventHandler(AssociatedObject_KeyUp);
}
}
TextBoxMyBehaviorは、インテリジェンスで出てこないように見えます。