开发者

How do prevent a WPF text box inside a combo from reacting to right mouse clicks?

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜