如何在 wpf c#visual studio 中为标签或文本块使用单击事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43542570/
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-13 14:04:10  来源:igfitidea点击:

How to use click event for label or textblock in wpf c# visual studio?

c#wpfvisual-studio

提问by zareen

I am working on desktop application i got suggestion to use wpf instead winforms. I want to go to another form/window when i click my label but i cant find click event for label and textblock? also can anyone tell me what is left mouse up event used for?

我正在开发桌面应用程序,我建议使用 wpf 代替 winforms。我想在单击标签时转到另一个表单/窗口,但找不到标签和文本块的单击事件?也有人能告诉我鼠标左键事件是做什么用的吗?

回答by mm8

also can anyone tell me what is left mouse up event used for?

也有人能告诉我鼠标左键事件是做什么用的吗?

It can for example be used to handle the click of a TextBlocklike you want:

例如,它可以用于处理TextBlock您想要的点击:

<TextBlock Text="..." MouseLeftButtonUp="TextBlock_MouseLeftButtonUp" />

This event occurs when the left mouse button is released while the mouse pointer is over this element as stated in the documentation: https://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseleftbuttonup(v=vs.110).aspx

如文档中所述,当鼠标指针位于此元素上方时释放鼠标左键时会发生此事件:https: //msdn.microsoft.com/en-us/library/system.windows.uielement.mouseleftbuttonup(v= vs.110).aspx

You could also handle the MouseLeftButtonDownevent: https://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseleftbuttondown(v=vs.110).aspx

您还可以处理该MouseLeftButtonDown事件:https: //msdn.microsoft.com/en-us/library/system.windows.uielement.mouseleftbuttondown(v=vs.110).aspx

回答by tabby

You can use PreviewMouseDown Event on TextBlock.

您可以在 TextBlock 上使用 PreviewMouseDown 事件。

<TextBlock x:Name="myTextBlock" PreviewMouseDown="TextBlock_Clicked"/>