2011-09-22 59 views
4

我正在使用堆棧面板來顯示列表框項目,但是當我決定將其更改爲虛擬化項目時,有時選定項目爲空。下面是我用調用選定項命令DataTemplate中的一部分:虛擬化堆棧面板返回空選定項目

 <i:Interaction.Triggers> 
      <ei:DataTrigger Binding="{Binding IsSelected}" Value="True"> 
       <i:InvokeCommandAction CommandParameter="{Binding}" 
         Command="{Binding DataContext.SelectItemCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}, Mode=FindAncestor}}" /> 
      </ei:DataTrigger> 
     </i:Interaction.Triggers> 

這裏是列表框:

<ListBox x:Name="_itemsListBox" 
         Grid.Row="1"              
         ScrollViewer.CanContentScroll="true" 
         ItemsSource="{Binding Items}" 
         IsSynchronizedWithCurrentItem="True" 
         SelectionMode="Single"             
         ScrollViewer.VerticalScrollBarVisibility="Visible" 
         ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
         ItemTemplate="{StaticResource ListItemTemplate}"> 

        <ListBox.ItemContainerStyle> 
         <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> 
          <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
         </Style> 
        </ListBox.ItemContainerStyle>      
       </ListBox> 

如果我關掉虛擬化,這個問題不會發生。我怎樣才能防止它返回一個空項目?

回答

0

我最好的猜測是,當它在列表框中不在視圖中時,你選擇的項目是空的。

它是有道理的,我猜是因爲虛擬化,雖然它是一個奇怪的。

您的解決方案很可能是當您選擇的項目發生更改時,請確保將其放入視圖中。看看this question來排序。

祝你好運& HTH