windows phone listbox的点击事件
前台
<ListBox x:Name="listbox1" Margin="6"> <ListBox.ItemTemplate> <DataTemplate> <Grid Margin="15" Tag="{Binding ImageID}" Tap="Post_Click"> <StackPanel Orientation="Horizontal"> <Image Source="{Binding Image}" Width="150" Height="120" Stretch="Fill"/> <TextBlock Text="{Binding ImageName}" FontSize="30" TextWrapping="Wrap" Width="300"/> </StackPanel> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
后台
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject { var count = VisualTreeHelper.GetChildrenCount(parentElement); if (count == 0) return null; for (int i = 0; i < count; i++) { var child = VisualTreeHelper.GetChild(parentElement, i); if (child != null && child is T) { return (T)child; } else { var result = FindFirstElementInVisualTree<T>(child); if (result != null) return result; } } return null; }
需要绑定
private void Post_Click(object sender, System.Windows.Input.GestureEventArgs e) { var selectedIndex = listbox1.SelectedIndex; ListBoxItem item = listbox1.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as ListBoxItem; StackPanel border = FindFirstElementInVisualTree<StackPanel>(item); Image img = FindFirstElementInVisualTree<Image>(item); TextBlock txtBlock = FindFirstElementInVisualTree<TextBlock>(item); MessageBox.Show(txtBlock.Text.ToString()); }
作者:眾尋
出处:https://www.cnblogs.com/ZXdeveloper/p/3583343.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
分类:
WP8
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· 长文讲解 MCP 和案例实战
· Hangfire Redis 实现秒级定时任务,使用 CQRS 实现动态执行代码
· Android编译时动态插入代码原理与实践
· 解锁.NET 9性能优化黑科技:从内存管理到Web性能的最全指南
· 一天 Star 破万的开源项目「GitHub 热点速览」
· 别再堆文档了,大模型时代知识库应该这样建
· 瞧瞧别人家的日期处理,那叫一个优雅!
· C#/.NET/.NET Core技术前沿周刊 | 第 35 期(2025年4.14-4.20)
· 在Avalonia/C#中使用依赖注入过程记录