如何点击DataTemplate中的TextBlock?
这是我的 XAML 代码:
<ListBox ItemsSource="{Binding}" Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Width="370">
<TextBlock Text="{Binding AuthorName}" x:Name="author" MouseEventLeftDown="click"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
点击处理程序
private void click(object sender, RoutedEventArgs e)
{
if(author.Text.Equals("Hi"))
{
// Do Something Special
}
}
错误是:
错误:名称“作者”在当前上下文中不存在
但我不明白导致此错误的原因或发生的原因。
This is my XAML code:
<ListBox ItemsSource="{Binding}" Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Width="370">
<TextBlock Text="{Binding AuthorName}" x:Name="author" MouseEventLeftDown="click"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And the Click Handler
private void click(object sender, RoutedEventArgs e)
{
if(author.Text.Equals("Hi"))
{
// Do Something Special
}
}
The error is:
Error: The name 'author' does not exist in the current context
But I don't understand what is causing this error or why it is occurring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的名称为
author
的 TextBlock 不存在于您的点击处理程序范围内,因为它位于 DataTemplate 中。发生的情况是,为每个数据项(大概是 Author 类或某种 Book 类)创建一次author
TextBlock ,因此您实际上可以拥有数十个名为author
的控件。您最好将点击处理程序中的
sender
转换为文本框,然后检查其文本属性。像这样的事情:Your TextBlock with the Name
author
doesn't exist in the scope of your click handler because it's in a DataTemplate. What's happening is that theauthor
TextBlock is created once for every one of your data items (Presumably an Author class or a Book class of some kind), so you literally can have dozens of controls namedauthor
.You are better off casting your
sender
in your click handler to a text box and then checking its text property. Something like this:最好使用专为触摸设计的 UI 元素,例如 HyperlinkButton 或 Button。您可以以任何您想要的方式设计它们 - 特别是如果您使用 Expression Blend - 但包含一些有关 Touch 的视觉反馈是很好的设计。
另外 - 我不确定您的
==
代码 - 您正在将发件人(UI 元素)与某些字符串表达式进行比较?It's probably better to use a UI element designed for touch, such as a HyperlinkButton or a Button. You can style these any way you would like to - especially if you use Expression Blend - but it is good design to include some visual feedback about the Touch.
Also - I'm not sure about your
==
code - you're comparing the sender (a UI element) against some string expression?首先,您的 TextBlock 在 DataTemplate 中定义;在 TextBlock 上尝试使用
x:Name
而不是Name
。其次,单击 TextBlock 可能非常棘手,因为您必须按下 TextBlock 中的精确像素。为了更轻松地单击您的 TextBlock,您可能需要在
TextBlock
上放置一个Background
,这样单击起来就会容易得多。您甚至可以使背景透明:First off, your TextBlock is defined in a DataTemplate; try
x:Name
instead ofName
on your TextBlock.Secondly it might be quite tricky to click your TextBlock since you will have to press an exact pixel in your TextBlock. To make it easier to click your TextBlock you might want to put a
Background
on yourTextBlock
, so it will be a lot easier to click. You can even make the background transparent:使用手势侦听器创建事件处理程序,例如“tap”或“double”或其他任何内容。
use the gesture listener for create an event handler like "tap" or double" or whatever.
用这个...
Use this...