How to a add a command to a WPF TextBlock?

Shai UI picture Shai UI · Aug 9, 2011 · Viewed 43.9k times · Source

I'd like to be able to click a textblock and have it run a Command. Is this possible? (if not do I just somehow make a tranparent button over it or something?)

Answer

Kris  picture Kris · Aug 10, 2011

You can use a InputBinding.

<TextBlock Text="Hello">
    <TextBlock.InputBindings>
        <MouseBinding Command="" MouseAction="LeftClick" />
    </TextBlock.InputBindings>
</TextBlock>

Edit: Hyperlink is probably worth a mention too.

<TextBlock><Hyperlink Command="" TextDecorations="None" Foreground="Black">Hello</Hyperlink></TextBlock>