虚拟化堆栈面板返回空选择项目



我使用堆栈面板来显示列表框项,但是当我决定将其更改为虚拟化面板时,所选项有时为空。下面是我用来调用所选项命令的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:

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

如果我关闭虚拟化,这个问题就不会发生。我怎样才能防止它返回一个空项?

也许你可以在Binding中定义一个FallBackValue,以返回FallBackValue而不是null

我最好的猜测是,当您选择的项目在列表框中超出视图时,它是空的。

我想这是有道理的,因为它是虚拟化的,尽管它是一个奇怪的。

您的解决方案可能是,当您选择的项目发生变化时,您确保将其带入视图。看一看这个问题,把它整理出来。

祝你好运!HTH

最新更新