显示可点击文本 WPF
我想在 GUI 中显示一些文本,并让用户能够双击它。 我想抓住这个事件并处理它。
我想这样做:
<TextBlock
Height="39"
TextElement.FontSize="18"
FontFamily="Verdana"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Name="Filelink"
Padding="5,0,0,0"
TextDecorations="Underline"
Text="{Binding Path=FilePath}"/>
但似乎处理 TextBlock
中的点击并不容易。
任何想法什么是呈现可点击文本的最佳方式。
谢谢。
I want to present some text in the GUI and give the user that ability to double click it.
I want to catch this event and deal with it.
I thought to do it like this :
<TextBlock
Height="39"
TextElement.FontSize="18"
FontFamily="Verdana"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Name="Filelink"
Padding="5,0,0,0"
TextDecorations="Underline"
Text="{Binding Path=FilePath}"/>
But seems that it's not easy to deal with clicks in TextBlock
.
Any ideas what is the best way to present a click able text.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想要可点击的文本,您只需重新设置
Button
的样式:另请参阅 这个问题。
If you want clickable text you can just restyle a
Button
:Also see this question.
您可以在文本块中嵌入超链接,如本示例所示
您还可以处理超链接 Click 事件来调用 Navigate
You can embed a hyberlink in a Textblock as shown in this example
You can also handle the hyperlinks Click event to call Navigate for example
为什么不使用
Label
并监听MouseDoubleClick
事件(尽管我同意 Xin 关于可用性的评论)?Why don't you just use a
Label
and listen for theMouseDoubleClick
event (although I do agree with Xin's comment about usability)?如果使用
Label
或Hyperlink
在您的情况下不起作用,您可以采取创建新的派生TextBlock
的方法,该方法只需定义一个新的DoubleClick
路由事件,该事件在树中向上冒泡:该控件的使用方式与标准
TextBlock
相同。在此示例中,双击TextBlock
将引发DoubleClick
事件,然后由父StackPanel
执行该事件以启动动画:Hope这有帮助!
If using a
Label
or aHyperlink
won't work in your situation, you could take the approach of a creating a new derivedTextBlock
that simply defines a newDoubleClick
routed event which bubbles up through the tree:This control can be used in the same way as your standard
TextBlock
. In this example, double clicking on theTextBlock
will raise theDoubleClick
event which is then acted upon by the parentStackPanel
to start an animation:Hope this helps!