SelectionChanged Event for ListBox in panorama.ItemTemplate for Windows Phone?
I have item template for the panorama control. In that template, i have listbox with listItem template. I have problem with selection changed event in the list box.
<phone:PhoneApplicationPage.Resources>
<CollectionViewSource x:Key="SlideItemList" Filter="collectionView_Filter"/>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--Panorama control-->
<controls:Panorama x:Name="AppPano" ItemsSource="{Binding SlidesCollections}" SelectionChanged="AppPano_SelectionChanged" >
<controls:Panorama.Background>
<ImageBrush ImageSource="PanoramaBackground.png"/>
</controls:Panorama.Background>
<controls:Panorama.ItemTemplate>
<DataTemplate>
<Grid VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,-100,0,0">
<StackPanel HorizontalAlignment="Center" Height="250" Width="200" VerticalAlignment="Top">
<TextBlock Text="{Binding Title}" HorizontalAlignment="Center" FontSize="200" Width="Auto"/>
</StackPanel>
<ListBox x:Name="ItemsList" ItemsSource="{Binding Source={StaticResource SlideItemList}}" Margin="0,250,0,0" VerticalAlignment="Top" SelectionChanged="ItemsList_SelectionChanged" Height="430">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="ImgStack" HorizontalAlignment="Left" Height="430" VerticalAlignment="Top" Width="370" Margin="50,0,0,0">
<Image Height="350" Width="360" Source="{Binding Image}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</controls:Panorama.ItemTemplate>
</controls:Panorama>
</Grid>
Xaml.cs
private void keyItemsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listbox = (ListBox)sender;
var conGen = listbox.ItemContainerGenerator;
var item = (UIElement)conGen.ContainerFromIndex(listbox.SelectedIndex);
if (item != null)
{
int selectedItemList = listbox.SelectedIndex;
if (sLasListItem != selectedItemList)
{
// navigate to another page
sLasListItem = selectedItemList;
}
}
}
Binding UI elements works perfectly.
Problem: 1. When i select new item from the list in one panorama item page, it will fire, the same selection changed event for all panorama items.
Ex: Let's consider, i have 4 panorama items. I selected 2nd item from the 1st panorama item list box. this selection changed event executed 4 times.
My expectation was, when i select new item from the list, this event should fire only one time for the corresponding panorama item.
Pls suggest me, how to do it...
It's because you're binding the same list 4 times. (Assuming that SlidesCollections
contains 4 items.)
Because each list is the same data, when you change the selected item in one view of that data, it is actually changed in the underlying (albeit filtered) list.
You should look to create separate lists in the view model instead.
链接地址: http://www.djcxy.com/p/59896.html下一篇: 用于Windows Phone的panorama.ItemTemplate中的ListBox的SelectionChanged事件?