WPF单击事件处理程序获取文本块文本 [英] WPF click event handler get textblock text
本文介绍了WPF单击事件处理程序获取文本块文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的xaml中有一个文本块:
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);
}
我收到了NullReferenceException.
I'm getting a NullReferenceException.
推荐答案
var tb = sender as TextBox
这将导致 null
,因为它实际上是一个 TextBlock
.
This results in null
because it's actually a TextBlock
.
只需更改为
var tb = sender as TextBlock
这篇关于WPF单击事件处理程序获取文本块文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文