如何添加一个命令到WPF TextBlock
本文关键字:命令 WPF TextBlock 一个 何添加 添加 | 更新日期: 2023-09-27 18:05:06
我想能够点击一个文本块,并让它运行命令。这可能吗?(如果不是,我只是在它上面做一个透明的按钮或什么?)
你可以使用InputBinding
<TextBlock Text="Hello">
<TextBlock.InputBindings>
<MouseBinding Command="" MouseAction="LeftClick" />
</TextBlock.InputBindings>
</TextBlock>
编辑:超链接可能也值得一提。
<TextBlock><Hyperlink Command="" TextDecorations="None" Foreground="Black">Hello</Hyperlink></TextBlock>
你不做一个透明的按钮,你把TextBlock 放到它:
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<TextBlock Text="Lorem Ipsum"/>
</Button>
按钮会消耗你的点击,点击不会进一步到你的TextBlock
。如果你不需要这个,这是一种方法。你可以修改文本块ControlTemplate,并添加按钮,给按钮一个新的带有透明矩形的ControlTemplate。一个更好的解决方案是使用一种方法将命令连接到EventBehavior之类的事件,并将其放在OnMouseLeftButtonDown
事件上。