WPF click event handler get textblock text

c#, event-handling, wpf

Solution

var tb = sender as TextBox

This results in `null` because it's actually a `TextBlock`.

Just change to

var tb = sender as TextBlock

Problem

I have a text block in my xaml: ``` <DataTemplate x:Key="InterfacesDataTemplate" DataType="ca:Interface"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock Grid.Column="1" Text="{Binding Path=Name}" MouseLeftButtonDown="interface_mouseDown"/> </Grid> </DataTemplate> ``` On the code behind I have an event handler for click (double-click) ``` private void interface_mouseDown(object sender, MouseButtonEventArgs e) { var tb = sender as TextBox; if (e.ClickCount == 2) MessageBox.Show("Yeah interfac " + tb.Text); } ``` I'm getting a NullReferenceException.

Original source