Search code examples
c#wpfxamlcommandtextblock

How to a add a command to a WPF TextBlock?


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?)


Solution

  • 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>