我正在使用堆栈面板来显示列表框项目,但是当我决定将其更改为虚拟化面板时,有时所选项目为空。这是我用来调用所选项目命令的 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>
如果我关闭虚拟化,则不会发生此问题。如何防止它返回 null 项?