De Bruce Lee
De Bruce Lee

Reputation: 303

How to add an event when I click on a TextBlock?

Can I know how to add an event when I click on a TextBlock? I can't find the onClick on the TextBlock. Does anyone know what is the name of the event?

code:

<TextBlock Name="Title" Click="?" />

Upvotes: 30

Views: 39077

Answers (2)

MovGP0
MovGP0

Reputation: 7765

You can use an InputBinding:

<TextBlock Text="{Binding SomeText}">
    <TextBlock.InputBindings>
        <MouseBinding Command="{Binding SomeCommand}" MouseAction="LeftClick" />
    </TextBlock.InputBindings>
</TextBlock>

Source: https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.inputbinding

Upvotes: 13

Nerdynosaur
Nerdynosaur

Reputation: 1888

Just use the "PreviewMouseDown" event. Good luck!

Upvotes: 40

Related Questions