Click events in Windows Phone ListBox

Source: Internet
Author: User



Front desk


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


Background


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;
}


Need to bind


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());
}


Click events in Windows Phone ListBox


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.