gpt4 book ai didi

c# - 单击 WPF richtextbox 中的 TextBlock

转载 作者:太空狗 更新时间:2023-10-30 01:05:40 28 4
gpt4 key购买 nike

我有这样一个流程文档:

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;

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

    <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 的事件处理程序被调用并正常工作 (Changing inline in flowdocument),但 TextBlock 的事件处理程序不是。

我做错了什么?谢谢

最佳答案

引自 MSDN :

Important

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.

因此您需要寻找替代方案。我可以推荐一些。

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

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

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

<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 设置事件处理程序:

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

para.MouseLeftButtonDown += new MouseButtonEventHandler(TextBlock_Click);

编辑

Adam Nathan 的书 WPF 4 Unleashed 中的引述:

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添加BlockUIContainer属性设置为 true(在 RichTextBox 中),否则事件将无法正常工作。

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

请参阅有关RichTextBox 的精彩教程here .

关于c# - 单击 WPF richtextbox 中的 TextBlock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17908449/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com