如何点击DataTemplate中的TextBlock?

发布于 2024-10-20 22:31:33 字数 823 浏览 1 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

帅的被狗咬 2024-10-27 22:31:33

您的名称为 authorTextBlock 不存在于您的点击处理程序范围内,因为它位于 DataTemplate 中。发生的情况是,为每个数据项(大概是 Author 类或某种 Book 类)创建一次 author TextBlock ,因此您实际上可以拥有数十个名为 author 的控件。

您最好将点击处理程序中的 sender 转换为文本框,然后检查其文本属性。像这样的事情:

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

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

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 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.

You are better off casting your sender in your click handler to a text box and then checking its text property. 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!
  }
}
傲娇萝莉攻 2024-10-27 22:31:33

最好使用专为触摸设计的 UI 元素,例如 HyperlinkBut​​ton 或 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?

岁吢 2024-10-27 22:31:33

首先,您的 TextBlock 在 DataTemplate 中定义;在 TextBlock 上尝试使用 x:Name 而不是 Name

其次,单击 TextBlock 可能非常棘手,因为您必须按下 TextBlock 中的精确像素。为了更轻松地单击您的 TextBlock,您可能需要在 TextBlock 上放置一个 Background,这样单击起来就会容易得多。您甚至可以使背景透明:

Background="Transparent"

First off, your TextBlock is defined in a DataTemplate; try x:Name instead of Name 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 your TextBlock, so it will be a lot easier to click. You can even make the background transparent:

Background="Transparent"
椒妓 2024-10-27 22:31:33

使用手势侦听器创建事件处理程序,例如“tap”或“double”或其他任何内容。

use the gesture listener for create an event handler like "tap" or double" or whatever.

来日方长 2024-10-27 22:31:33

用这个...

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

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

Use this...

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

        if (author.Text.Equals("Hi"))
        {
            // Do Something Special    
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文