您可擴展的文本框
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