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>

如果我关闭虚拟化,则不会发生此问题。如何防止它返回 null 项?

4

2 回答 2

0

也许您可以在 Binding 中定义一个 FallBackValue,以取回 FallBackValue 而不是 null。

于 2011-09-23T10:27:21.447 回答
0

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

我猜这是有道理的,因为虚拟化虽然很奇怪。

您的解决方案很可能是,当您选择的项目发生更改时,您确保将其带入查看。看看这个问题来解决这个问题。

祝你好运

于 2011-09-30T11:20:51.400 回答