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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:35:09  来源:igfitidea点击:

How to add an event when I click on a TextBlock?

wpf

提问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