Reputation: 3703
I am working on windows phone 7 using VS 2010 Express edition for windows phone.I have grid of images made using wrappanel .When i select any item or image ,i want the id of the clicked image.Also similarly have a list box and when any list item click, i again want the value of text block clicked i.e the text. I am using this method for listbox item selection,an want the id here:
private void on_selection(object sender, SelectionChangedEventArgs e)
{
//want id of clicked item here
}
void grid_image_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
//want id of clicked image in grid here
}
Any suggestions are welcome.
The xml for listbox is :
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="768"/>
<RowDefinition Height="0*" />
</Grid.RowDefinitions>
<ListBox Name="Index_list" SelectionChanged="on_selection">
</ListBox>
<Image Visibility="Collapsed" Margin="0,151,0,200" Name="selected_image"></Image>
</Grid>
Upvotes: 0
Views: 1066
Reputation: 65586
Assuming the following XAML:
<ListBox Name="Index_list" SelectionChanged="on_selection">
<!-- These items could also be added in code -->
<TextBlock Text="list box option 1" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="list box option 2" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="list box option 3" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="list box option 4" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="list box option 5" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="list box option 6" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="list box option 7" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="list box option 8" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="list box option 9" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="list box option 10" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="list box option 11" Style="{StaticResource PhoneTextExtraLargeStyle}" />
</ListBox>
you can get at the text from the selected TextBlock in the following ways.
(Note that use of a MessageBox is purely for demonstration.)
private void on_selection(object sender, SelectionChangedEventArgs e)
{
// As the listbox is named we can do this:
if (Index_list.SelectedIndex >= 0)
{
MessageBox.Show((Index_list.SelectedItem as TextBlock).Text);
}
// if the listbox wasn't named we could do this:
if (sender is ListBox) // always good to double check
{
var sal = sender as ListBox;
if (sal.SelectedIndex >= 0)
{
MessageBox.Show((sal.SelectedItem as TextBlock).Text);
}
}
// Or we could use the EventArgs:
if (e.AddedItems.Count == 1)
{
MessageBox.Show((e.AddedItems[0] as TextBlock).Text);
}
}
Upvotes: 1
Reputation: 52675
Have you tried using the e.AddedItems
property of the SelectionChangedEventArgs from the event args.
As the docs say this will give you the
The items that were selected since the last time the SelectionChanged event occurred.
Specifically it will give you an IList of the selected bound objects. This doesn't give you the index but once you have the selected items its prettys easy to get the index if that's what you really want (e.g IndexOf on what you've bound to).
You could also cast the sender to a list box and then inspect the SelectedIndex but that's problematic with a list box.
Upvotes: 2