WP8 点击图片获取长列表选择器中的所选项目 [英] WP8 Tap the image to get the selected item in longlistselector

查看:17
本文介绍了WP8 点击图片获取长列表选择器中的所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有文本块和图像的 LongListSelector,然后如何点击图像以在我的列表中显示所选项目名称,然后点击项目名称以显示消息框?下面是我绑定名称和图片的代码:

I created a LongListSelector with textblock and an image,then How do I tap the image to show selected item name in my list, and tap the item name to show messagebox? Below is my code to bind the name and image:

<phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="TileDataTemplate">
            <Grid Background="{StaticResource TransparentBrush}"
                  Margin="0, 0, 0, 12" Height="60">
                <TextBlock Text="{Binding Name}" Margin="60, 10, 0, 0" FontSize="24" Height="60">
                </TextBlock>
                <Image x:Name="GetName" Tap="GetName_Tap" Grid.Column="0" Source="/Assets/AppBar/Delete.png" Height="40" Width="40"
                                Margin="0, 6, 0, 5" HorizontalAlignment="Right" VerticalAlignment="Top" />
            </Grid>
        </DataTemplate>
</phone:PhoneApplicationPage.Resources>

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <phone:LongListSelector
                    SelectionChanged="MainLongListSelector_SelectionChanged"
                    Margin="10,6,0,0"
                    ItemsSource="{Binding Staff.Items}"
                    LayoutMode="Grid"
                    GridCellSize="400,80"
                    ItemTemplate="{StaticResource TileDataTemplate}"
                    />
    </Grid>

背后的代码:

private void MainLongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   MessageBox.Show("Hi");
}

private void GetName_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
   LongListSelector selector = sender as LongListSelector;
   StaffData data = selector.SelectedItem as StaffData;
   MessageBox.Show(data.Name);
}

当我点击文本块时,成功显示消息嗨".但是如果我点击图像,我会得到空值.我该如何解决?谢谢

When I tap on textblock,the message "Hi" is displayed successful. But if I tap on image, I get the null value. How to I solve it? Thanks

推荐答案

sender 不是 LongListSelector 而是用户点击的图像,因此出现空错误.

sender isn't the LongListSelector but the image on which the user tapped, hence the null error.

基本上,您只想检索用户点击的项目?在这种情况下,使用被点击控件的 DataContext 属性来检索它:

Basically, you just want to retrieve the item on which the user has tapped? In that case, use the DataContext property of the tapped control to retrieve it:

private void GetName_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
   var element = (FrameworkElement)sender;
   StaffData data = (StaffData)element.DataContext;
   MessageBox.Show(data.Name);
}

(FrameworkElement 是每个 UI 控件的基本类型.使用它,您不必担心它是图像、文本块还是...)

(FrameworkElement is the base type of every UI control. Using that, you don't have to worry about whether it's an image, a textblock, ...)

这篇关于WP8 点击图片获取长列表选择器中的所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆