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

发布于 2024-12-06 12:32:05 字数 1689 浏览 0 评论 0原文

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

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

I was using a stack panel to display listbox items, but when I decided to change it to a virtualizing one the selected item was null sometimes. Here is part of the DataTemplate I was using to invoke the selected item command:

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

Here is the 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>

If I turn off virtualization, this issue doesn't happen. How can I prevent it from returning a null item?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初心未许 2024-12-13 12:32:05

Maybe you can define a FallBackValue inside your Binding, to get back the FallBackValue instead of null.

感情废物 2024-12-13 12:32:05

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

我认为这是有道理的,因为虚拟化,尽管它很奇怪。

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

祝你好运&华泰

My best guess would be that your selected item is null when it is out of view in the listbox.

It makes sense I guess because of the virtualization though it is an odd one.

Your solution is likely to be that when your selected item changes you make sure you bring it in to view. Take a look at this question to sort that out.

Good luck & HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文