0

Is there anyway to add right click events to all textbox controls in silverlight without needing to manually adding it to each control in the whole project?

doing like:

<TextBox x:Name="txtName" MouseRightButtonUp="txtName_MouseRightButtonUp" 
    MouseRightButtonDown="txtName_MouseRightButtonDown" /></TextBox>

then fixing the events in the .cs for about 50+ (hopefully it's just 50+) textboxes can take a while.

If not then what might be the easiest way to do this?

Bahamut
  • 1,929
  • 8
  • 29
  • 51

2 Answers2

1

My answer to this question is also the answer to your question.

In short it's probably easiest to derive a type from TextBox, put your MouseRightButtonDown event handler in there and replace all existing instances of textBox with your type.

Community
  • 1
  • 1
Silver Solver
  • 2,310
  • 1
  • 13
  • 19
1

You can extend your textbox

class SimpleTextBox
{
    public SimpleTextBox()
    {
        DefaultStyleKey = typeof (SimpleCombo);
        MouseRightButtonDown += OnMouseRightButtonDown;
    }

    private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs   
mouseButtonEventArgs)
    {
        //TODO something
    }
}

==========

And use this control. Or as alternative solution - you can create behavior:

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>   

And attach this handler to your TextBox general style.

Bahamut
  • 1,929
  • 8
  • 29
  • 51
Roman Badiornyi
  • 1,509
  • 14
  • 28
  • Doesn't work until I added the tags to specify the behavior. Looks like I'll have to do that one manually. I was thinking of some way so that other coders wouldn't need to do add those. – Bahamut Feb 19 '13 at 07:42
  • ok i jest. i updated the post and added the link to a post describing how to add behaviors to styles. – Bahamut Feb 19 '13 at 10:29
  • while the poster still hasn't accepted my edits. update: see http://stackoverflow.com/questions/13498216/attach-behaviour-to-all-textboxes-in-silverlight you may ignore the xaml code and proceed to using styles instead – Bahamut Feb 19 '13 at 12:15
  • Yes, I meant that you should attach behavior in style, but had mistake and wrote "handler" instead of "behavior" :). Thanks for updating. – Roman Badiornyi Feb 19 '13 at 14:21