wpf 单击 TextBlock 时如何添加事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13562474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add an event when I click on a TextBlock?
提问by De Bruce Lee
Can I know how to add an event when I click on a TextBlock? I can't find the onClick on the TextBlock. Does anyone know what is the name of the event?
我可以知道如何在单击 TextBlock 时添加事件吗?我在 TextBlock 上找不到 onClick。有谁知道这个活动叫什么名字?
code:
代码:
<TextBlock Name="Title" Click="?" />
回答by 0070
Just use the "PreviewMouseDown" event. Good luck!
只需使用“PreviewMouseDown”事件。祝你好运!
回答by MovGP0
You can use an InputBinding
:
您可以使用InputBinding
:
<TextBlock Text="{Binding SomeText}">
<TextBlock.InputBindings>
<MouseBinding Command="{Binding SomeCommand}" MouseAction="LeftClick" />
</TextBlock.InputBindings>
</TextBlock>
Source: https://docs.microsoft.com/en-us/dotnet/api/system.windows.input.inputbinding
来源:https: //docs.microsoft.com/en-us/dotnet/api/system.windows.input.inputbinding