2012-02-29 39 views
1

我有全景控件的項目模板。在那個模板中,我有listItem模板的列表框。我在列表框中選擇已更改的事件有問題。針對Windows Phone的panorama.ItemTemplate中的ListBox的SelectionChanged事件?

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

綁定UI元素完美的作品。

問題: 1.當我從一個全景項目頁面的列表中選擇新項目時,它將觸發,相同的選擇會更改所有全景項目的事件。

例如: 讓我們考慮一下,我有4個全景項目。我從第一個全景項目列表框中選擇第二個項目。這個選擇改變了4次執行的事件。

我的期望是,當我從列表中選擇新項目時,此事件應該只觸發一次相應的全景項目。

PLS建議我,該怎麼辦呢?

+0

確實每個全景控制和每個列表框都有分配給他們的唯一selectionshanged事件? – earthling 2012-02-29 22:29:40

回答

1

這是因爲你的4倍結合相同的列表。 (假設SlidesCollections包含4個項目。)

因爲每個列表是相同的數據,當在該數據的一個視圖改變所選擇的項目,它實際上是在底層(儘管經濾波的)列表改變。

您應該看看在視圖模型中創建單獨的列表。

+0

非常感謝!即使有同樣的問題,並根據您的答案,我可以解決它! @作者:請將此標記爲解決問題的答案 – 2016-07-29 12:37:26

相關問題