单击 WPF 富文本框中的 TextBlock [英] Clicking on a TextBlock in WPF richtextbox

查看:119
本文介绍了单击 WPF 富文本框中的 TextBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的流程文件:

I have a flowdocument like this:

var mcFlowDoc = new FlowDocument();
var para = new Paragraph();
para.Inlines.Add(textBlock1);
para.Inlines.Add(textBlock2);
para.Inlines.Add(textBlock3);
mcFlowDoc.Blocks.Add(para);
richTextBox1.Document = mcFlowDoc;

我需要一个事件来触发鼠标点击文本块:

and I need an event to trigger on mouse click on a textblock:

    <RichTextBox Margin="10,10,230,12" Name="richTextBox1" FontFamily="Simplified Arabic" FontSize="16" IsReadOnly="True" IsReadOnlyCaretVisible="False" ForceCursor="False" FlowDirection="RightToLeft" VerticalScrollBarVisibility="Auto">
        <RichTextBox.Resources>
            <Style TargetType="Run">
                <EventSetter Event="MouseLeftButtonDown" Handler="Run_Click" />
            </Style>
            <Style TargetType="TextBlock">
                <EventSetter Event="MouseLeftButtonDown" Handler="TextBlock_Click" />
            </Style>
        </RichTextBox.Resources>
    </RichTextBox>

void TextBlock_Click(object sender, MouseButtonEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
        }

Run 的事件处理程序被调用并正常工作(在流文档中更改内联),但对于 TextBlock 来说不是.

The event handler for Run is called and works properly (Changing inline in flowdocument) , but that for TextBlock isn't.

我做错了什么?谢谢

推荐答案

引用自 MSDN:

重要

RichTextBox 具有对冒泡 MouseUp 和 MouseDown 事件的内置处理.因此,永远不会调用从 RichTextBox 侦听 MouseUp 或 MouseDown 事件的自定义事件处理程序.如果您需要响应这些事件,请改为监听隧道 PreviewMouseUp 和 PreviewMouseDown 事件,或者使用 HandledEventsToo 参数注册处理程序(后一个选项只能通过代码获得).不要将事件标记为已处理,除非您有意禁用 RichTextBox 对这些事件的本机处理,并注意这会对控件的 UI 产生显着影响.

RichTextBox has built-in handling for the bubbling MouseUp and MouseDown events. Consequently, custom event handlers that listen for MouseUp or MouseDown events from a RichTextBox will never be called. If you need to respond to these events, listen for the tunneling PreviewMouseUp and PreviewMouseDown events instead, or register the handlers with the HandledEventsToo argument (this latter option is only available through code). Do not mark the event handled unless you deliberately want to disable RichTextBox native handling of these events, and be aware that this has notable effects on the control's UI.

所以你需要寻找替代品.我可以推荐几个.

So you need to look for alternatives. I can suggest a few.

首先,您可以为所有RichTextBox设置一个事件处理程序PreviewMouseDown:

First, you can set an event handler PreviewMouseDown for all RichTextBox:

<RichTextBox PreviewMouseDown="TextBlock_Click" ... />

其次,使用 BlockUIContainer 并将文本放入内容按钮中.例如:

Second, use the BlockUIContainer and put the text in the content button. For example:

<Paragraph FontSize="18">Flow Example</Paragraph>

<BlockUIContainer>
  <Button x:Name="MyButton" ClickMode="Release" Click="Button_Click">
    <TextBlock Margin="4" TextWrapping="Wrap">
      Some text
    </TextBlock>
  </Button>
</BlockUIContainer>

第三,您可以像这样为Paragraph设置事件处理程序:

Third, you can set the event handler for the Paragraph like that:

var para = new Paragraph();
para.Inlines.Add(textBlock1);

para.MouseLeftButtonDown += new MouseButtonEventHandler(TextBlock_Click);

编辑

引自 Adam Nathan 的著作 WPF 4 Unleashed:

Quote from Adam Nathan's book WPF 4 Unleashed:

TextBox 公开简单的整数属性,例如 CaretIndex、SelectionStart 和 SelectionEnd,而 RichTextBox 公开 TextPointer 类型的 CaretPosition 属性和 TextSelection 类型的 Selection 属性.此外,RichTextBox 的内容存储在 FlowDocument 类型的 Document 属性中,而不是简单的字符串 Text 属性中.内容甚至可以包含嵌入的 UIElements,如果 RichTextBox 的 IsDocumentEnabled 属性设置为 true,它们可以是交互式的并引发事件.

Whereas TextBox exposes simple integer properties such as CaretIndex, SelectionStart, and SelectionEnd, RichTextBox exposes a CaretPosition property of type TextPointer and a Selection property of type TextSelection. In addition, RichTextBox’s content is stored in a Document property of type FlowDocument rather than the simple string Text property. The content can even contain embedded UIElements, and they can be interactive and raise events if RichTextBox’s IsDocumentEnabled property is set to true.

事件开始起作用了,需要用IsDocumentEnabled 属性设置为 true(在 RichTextBox),否则该事件将无法正常工作.

Events began to work, it is necessary to add BlockUIContainer with IsDocumentEnabled property is set to true (in RichTextBox), otherwise, the event will not working altogether.

总的来说,我不明白你为什么需要 TextBlockRichTextBox 中.使用它的标准功能,它们几乎涵盖了那些 RunParagraph 等.如果它们不匹配,则没有理由使用 RichTextBox.

In general, I do not understand why you need it TextBlock inside RichTextBox. Use it the standard features, they pretty much cover, those Run, Paragraph, etc. If they do not match, then there is no reason to use RichTextBox.

请参阅关于 RichTextBox 的精彩教程 此处.

See the nice tutorial about RichTextBox here.

这篇关于单击 WPF 富文本框中的 TextBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆