首页 文章

如何在WPF RichTextBox中禁用鼠标右键的默认上下文菜单?

提问于
浏览
1

我有以下事件处理程序:

private void rtb_MouseDown(object sender, MouseEventArgs e)
{
    if (e.RightButton == MouseButtonState.Pressed)
    {
        // Get the nearest TextPointer to the mouse position. 
        TextPointer location = rtb.GetPositionFromPoint(Mouse.GetPosition(rtb), true);

        // Get the nearest word using this TextPointer. 
        TextRange word = GetWordRange(location);

        // Display the word. 
        tb.Text = word.Text;

        e.Handled = true;
    }
}

这连接到RichTextBox的PreviewMouseDown事件 . 触发此事件并调用上述方法,并且光标下的单词显示在单独的TextBox(称为tb)中 .

问题是,之后还会显示鼠标右键单击事件的默认上下文菜单(包含剪切/复制/粘贴选项) . 将Handled属性设置为true似乎没有帮助 . 如何禁用此上下文菜单?

编辑:Xaml代码:

<Window x:Class="rtbTest1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <RichTextBox Height="175" HorizontalAlignment="Left" Margin="10,127,0,0" Name="rtb" VerticalAlignment="Top" Width="483" PreviewMouseDown="rtb_MouseDown" />
        <TextBox Height="59" HorizontalAlignment="Left" Margin="286,24,0,0" Name="tb" VerticalAlignment="Top" Width="186" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="63,56,0,0" Name="btn1" VerticalAlignment="Top" Width="75" Click="btn1_Click" />
    </Grid>
</Window>

1 回答

  • 11

    null 它:

    <RichTextBox ContextMenu="{x:Null}"/>
    

相关问题