“可点击”文本块?
我有一个 WP7 应用程序,我希望有一个“可点击”的 TextBlock
区域,当用户单击 TextBlock
时,它会将其置于编辑模式(不同的控制)。 这将为用户在编辑文本之前添加另一个明确的步骤。
TextBlock
没有点击事件(这并不让我感到惊讶)。
有什么办法可以做到这一点吗?包裹在另一个控件或类似的东西中?
I have a WP7 app where I would like to have a "clickable" TextBlock
area, that when a user clicks on the TextBlock
, it puts it into edit mode (a different control).
This would add another explicit step for a user before editing text.
There is no click event for the TextBlock
(which doesn't surprise me).
Is there any way to do this? Wrapped in another control, or something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,有一个点击事件。它称为 MouseLeftButtonDown
Yes, there is a click event. Its called MouseLeftButtonDown
我通过在其模板内创建一个带有 TextBlock 的按钮解决了这个问题,如下所示:
它有更多的标记,但您不需要编写任何代码,它由框架提供并且可以正常工作。
I have solved this by creating a Button with the TextBlock inside its Template like this:
It's a bit more markup, but you don't need to write any code, it is provided by the framework and works properly.
您可以使用手势支持关联单击事件。
编辑:通过这样做,您的行为将与用户在操作过程中在显示屏上移动手指时的单击(点击)一致。单击在鼠标按下、移开控制、鼠标松开时不会触发。点击手势的作用相同。并非所有用户都知道他们可以通过移动鼠标/手指来中止单击,但有些用户知道,如果处理不当,您将陷入困境。 MouseLeftButtonUp 处理的工作方式相同,但我不建议依赖于此,因为它不是设计使然,并且可能会出现意外的更改或与其他控件发生冲突。
You can associate a click event using gesture support.
edit: by doing it this way your behaviour will be consistant with a Click (tap) with respect to the user moving their finger across the display during the operation. Click will not fire under mousedown, move off control, mouseup. Tap gesture works the same. Not all users know they can abort a click by moving their mouse/finger, but some do and you'll get caught out on this if not handling it correctly. MouseLeftButtonUp handling works the same way, but I wouldn't recommend relying on this as it's not by design and there may be unanticipated changes or conflicts with other controls.
有几个答案建议使用 MouseLeftButton 事件,但我看到很多这种方法出错的例子。
我建议将 TextBlock (或其他控件)放入具有自定义样式的按钮中(删除除内容控件之外的所有边框/边距)。好多了!
There are several answers which advice to use MouseLeftButton events, but I saw a lot of examples where this approach goes wrong.
I recommend to place TextBlock (or other control/controls) into a Button with a custom style (removed all borders/margins except content control). It's MUCH MUCH BETTER!
另一种方法是使用 Seva 的解决方案,但添加更多内容,这样滚动就不会触发“点击”
Another way of doing it is using Seva's solution but adding a bit more so scrolling won't trigger the 'Click'
听起来你只需要一个按钮。更改控件模板以删除边框样式等,并创建您自己的(或清除现有的)视觉状态。或者甚至对其进行编辑,使“压抑”状态产生一点 3D 投影。
此外,通过按钮,如果您使用 MVVM,还可以使用按钮上的命令。
卢克
It sounds like you just need a button. Change the control template to remove the border styling etc. and make your own (or clear the existing) visual states. Or even edit it so the 'depressed' state gives a little 3D projection.
Also, with a button, you can also use the Command on the button if you're using MVVM.
Luke
我更喜欢添加一个事件并动态执行其余事件。
I prefer to add one event and do the rest dynamically.
可以使用 PointerReleased 来捕获 UWP 控件的单击事件
Catch Click event for UWP Control can be done using PointerReleased