【发布时间】:2011-10-23 15:04:54
【问题描述】:
我希望能够单击文本块并让它运行命令。这可能吗? (如果不是我只是以某种方式在它上面制作一个透明按钮或其他什么?)
【问题讨论】:
标签: c# wpf xaml command textblock
我希望能够单击文本块并让它运行命令。这可能吗? (如果不是我只是以某种方式在它上面制作一个透明按钮或其他什么?)
【问题讨论】:
标签: c# wpf xaml command textblock
您可以使用InputBinding。
<TextBlock Text="Hello">
<TextBlock.InputBindings>
<MouseBinding Command="" MouseAction="LeftClick" />
</TextBlock.InputBindings>
</TextBlock>
编辑:超链接可能也值得一提。
<TextBlock><Hyperlink Command="" TextDecorations="None" Foreground="Black">Hello</Hyperlink></TextBlock>
【讨论】:
LeftDoubleClick 并得到了我需要的东西!
你没有在它上面做一个透明按钮,你把 TextBlock 放入它:
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<TextBlock Text="Lorem Ipsum"/>
</Button>
【讨论】:
MinHeight="0"。
那么按钮会消耗您的点击,并且点击不会进一步到达您的TextBlock。如果您不需要它,那将是一种方法。您可以修改文本块 ControlTemplate,并添加按钮,为按钮提供一个带有透明 RectangleT 的新 ControlTemplate。更好的解决方案是使用一种方法将命令连接到 EventBehavior 之类的事件,并将其放在 OnMouseLeftButtonDown 事件上。
【讨论】: