前台

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>
windows phone listbox的点击事件

 

后台

windows phone 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;
}
windows phone listbox的点击事件

需要绑定

windows phone listbox的点击事件
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());
}
windows phone listbox的点击事件

 

相关文章:

  • 2022-01-02
  • 2021-07-25
  • 2021-11-20
  • 2022-02-02
猜你喜欢
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2021-12-18
  • 2021-09-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案