2015-06-06 38 views
0

在我的Windows手機應用程序中,其中一個頁面包含LongListSelector。在那LongListSelector我正在使用一些轉換器。當加載LongListSelector時,轉換器將照常進行調用。但是,當我撥打ScrollTo()方法滾動到LongListSelector的特定項目時,轉換器將再次被調用。爲什麼再次調用轉換器? ScrollTo()方法是否會導致轉換器再次被調用?爲什麼轉換器被再次調用?

示例代碼:

XAML:

<phone:LongListSelector ItemTemplate="{StaticResource LLSItemSource}" 
         Name="ChatListBox"> 

    <phone:LongListSelector.ItemTemplate> 
     <DataTemplate> 
      <Grid Background="Transparent" 
        Visibility="{Binding ID, Converter={StaticResource visibilityConverter}}" 
        Margin="0,6"> 

      <toolkit:ContextMenuService.ContextMenu> 
       <toolkit:ContextMenu> 
        <toolkit:MenuItem Header="delete" 
             Click="ContextMenuItem_Click" /> 
       </toolkit:ContextMenu> 
      </toolkit:ContextMenuService.ContextMenu> 

      <Border Background="Black" 
        HorizontalAlignment="Center" 
        Padding="30,5,30,8" 
        CornerRadius="20"> 
       <TextBlock Text="{Binding Date, Converter={StaticResource dateStringConverter}}" 
          Foreground="White" 
          FontSize="20" /> 
      </Border> 
     </Grid>  
    </DataTemplate> 
</phone:LongListSelector.ItemTemplate> 

CS:爲longlistselector項目

private void ContextMenuItem_Click(object sender, RoutedEventArgs e) 
{ 
    string header = (sender as MenuItem).Header.ToString(); 
    MessageModel selectedListBoxItem = (sender as MenuItem).DataContext as MessageModel; 

    if (selectedListBoxItem == null) 
     return; 

    if (header == "delete") 
    { 
     DeleteItemByID(selectedListBoxItem.ID); 

     if (ChatListBox.ItemsSource.Count > 0) 
     { 
      ChatListBox.ScrollTo(ChatListBox.ItemsSource[delIndex - 1]); // here I am scrolling to the last item that causes converters to be called again 
     } 
    } 
} 
+0

請分享代碼片段。 – Nishi

+0

我已經添加了一些代碼。 @Nishi請檢查。 – raisul

+0

沒有[很好,_minimal_,_complete_代碼示例](http://stackoverflow.com/help/mcve)清楚地說明了您的問題,因此無法回答任何「爲什麼?」。問題與任何具體細節。也就是說,最可能的原因是該列表是「虛擬化」的,這意味着實際顯示的元素是根據需要隨時創建的。所以你的轉換器也會根據需要被調用。無論如何,你絕對不應該假設你的轉換器何時或多久會被調用;框架可能需要調用它的原因有很多。 –

回答

0

你寫的轉換器。在加載選擇器中的每個項目時,它會執行轉換給定模板上的值

相關問題