views:

15

answers:

1

I creating a custonmized box class (inherits from ComboBox). I don't want the text box to react to right mouse clicks. I can get rid of the context menu by setting this to null in ApplyTemplate, but right mouse clicks move the cursor. I tried hooking up PreviewMouseRightButtonDown in ApplyTemplate and setting Handled to True, but the event still gets through which is strange as it seems to work for the left click.

+2  A: 

The cursor actually moves when the mouse button is released, so you want mark the MouseRightButtonUp event as handled. You could override OnMouseRightButtonUp:

protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
{
    base.OnMouseRightButtonUp(e);
    e.Handled = true;
}

Or you could attach a class handler to the MouseRightButtonUp event to mark it as handled:

static MyComboBox()
{
    EventManager.RegisterClassHandler(
        typeof(MyComboBox), 
        MouseRightButtonUpEvent, 
        new MouseButtonEventHandler(MyComboBox_MouseRightButtonUp));
}

private static void MyComboBox_MouseRightButtonUp(
    object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
}

That will also prevent the context menu from being created without you having to set it to null explicitly.

Quartermeister