您可扩展的文本框
class SimpleTextBox
{
public SimpleTextBox()
{
DefaultStyleKey = typeof (SimpleCombo);
MouseRightButtonDown += OnMouseRightButtonDown;
}
private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs
mouseButtonEventArgs)
{
//TODO something
}
}
==========
并使用此控制。 或作为替代解决方案 - 您可以创建行为:
CS: ... using System.Windows.Interactivity;
public class TextBoxBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.MouseRightButtonDown += AssociatedObject_MouseRightButtonDown;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.MouseRightButtonDown -= AssociatedObject_MouseRightButtonDown;
}
private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
e.Handled = true;
// DO SOMETHING
}
}
XAML:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<TextBox ...>
<i:Interaction.Behaviors>
<local:TextBoxBehavior />
</i:Interaction.Behaviors>
</TextBox>
,并附这个处理程序到你的文本框大将风范。
直到我添加了标记来指定行为后才起作用。看起来我必须手动执行该操作。我正在考虑某种方式,以便其他编码人员不需要添加这些编码。 – Bahamut 2013-02-19 07:42:58
好吧,我开玩笑。我更新了帖子,并将链接添加到描述如何将行为添加到样式的帖子中。 – Bahamut 2013-02-19 10:29:32
虽然海报仍然没有接受我的编辑。 更新:请参阅http://stackoverflow.com/questions/13498216/attach-behaviour-to-all-textboxes-in-silverlight您可以忽略xaml代码并继续使用样式代替 – Bahamut 2013-02-19 12:15:15