简体   繁体   English

如何单击DataTemplate中的TextBlock?

[英]How click a TextBlock in DataTemplate?

This is my XAML code: 这是我的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>

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. 但是我不明白是什么原因导致了这个错误,或者为什么会发生。

Your TextBlock with the Name author doesn't exist in the scope of your click handler because it's in a DataTemplate . 具有Name author TextBlock在您的单击处理程序范围内不存在,因为它位于DataTemplate中 What's happening is that the author 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 named author . 发生的事情是, author TextBlock为您的每个数据项(大概是Author类或Book类)创建了一次,因此您实际上可以有数十个名为author的控件。

You are better off casting your sender in your click handler to a text box and then checking its text property. 最好将点击处理程序中的sender投射到文本框中,然后检查其text属性。 Something like this: 像这样:

private void click(object sender, RoutedEventArgs args)
{
  var textBox = sender as TextBox;
  if(textBox == null)
     return;

  if(textBox.Text.Equals("hi"))
  {
     // Do Something Crazy!
  }
}

It's probably better to use a UI element designed for touch, such as a HyperlinkButton or a Button. 最好使用专为触摸设计的UI元素,例如HyperlinkBut​​ton或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. 您可以按照自己喜欢的方式设置样式(尤其是在使用Expression Blend时),但是最好包含一些有关Touch的视觉反馈。

Also - I'm not sure about your == code - you're comparing the sender (a UI element) against some string expression? 另外-我不确定您的==代码-您是否正在将发件人(UI元素)与某些字符串表达式进行比较?

First off, your TextBlock is defined in a DataTemplate; 首先,您的TextBlock是在DataTemplate中定义的; try x:Name instead of Name on your TextBlock. 在您的TextBlock上尝试使用x:Name而不是Name

Secondly it might be quite tricky to click your TextBlock since you will have to press an exact pixel in your TextBlock. 其次,单击TextBlock可能会非常棘手,因为您必须在TextBlock中按一个确切的像素。 To make it easier to click your TextBlock you might want to put a Background on your TextBlock , so it will be a lot easier to click. 为了使单击TextBlock更加容易,您可能需要在TextBlock上放置一个Background ,因此单击它会容易得多。 You can even make the background transparent: 您甚至可以使背景透明:

Background="Transparent"

使用手势侦听器创建事件处理程序,例如“ tap”或“ double”等。

Use this... 用这个...

private void click(object sender, RoutedEventArgs e)
    {
        var author = (TextBlock)sender;

        if (author.Text.Equals("Hi"))
        {
            // Do Something Special    
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM