列表框“IsSelected"绑定仅部分工作 [英] Listbox "IsSelected" binding only partially working

查看:25
本文介绍了列表框“IsSelected"绑定仅部分工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListBox,我通过绑定动态填充(这是在 DataTemplate 中定义的,这就是绑定有点不寻常的原因):

I have a ListBox that I populate dynamically via a binding (this is defined in a DataTemplate, which is why the binding is somewhat unusual):

<ListBox SelectionMode="Extended" ItemsSource="{Binding DataContext.ResultList, RelativeSource={RelativeSource AncestorType=Window}}">
  <ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
      <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
    </Style>
  </ListBox.ItemContainerStyle>
  <ListBox.ItemTemplate>
    <DataTemplate>
      <Label Content="{Binding Object}"/>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

每个 ListBoxItemIsSelected 属性都绑定到自定义对象的 IsSelected 属性.

Each ListBoxItem's IsSelected property is bound to an IsSelected property on a custom object.

当我选择单个 ListBoxItem 时,绑定工作正常 - 自定义对象的 IsSelected 属性在我的 ViewModel 中更新.但是,如果我使用 Ctrl+A 命令选择所有 ListBoxItem,则只有当前可见的 ListBoxItem(当前在我的滚动视口中的那些)会更新其 ViewModel绑定.在前端,所有 ListBoxItem 似乎都被选中,容器 ListBox 上的 ListBox.SelectedItems.Count 属性显示所有项目被选中.

When I select individual ListBoxItems, the binding works properly - the custom object's IsSelected property is updated in my ViewModel. However, if I select all of the ListBoxItems with a Ctrl+A command, only the currently visible ListBoxItems (those that are currently in my scrolling viewport) update their ViewModel bindings. On the frontend, all the ListBoxItems appear to be selected, and the ListBox.SelectedItems.Count property on the container ListBox shows that all items are selected.

此外,当我使用 Ctrl+A 选择所有 ListBoxItem 后滚动浏览 ListBox 时,当每个 ListBoxItem滚动到视图中.

Furthermore, as I scroll through the ListBox after selecting all ListBoxItems with Ctrl+A, the bindings are successfully updated when each ListBoxItem is scrolled into view.

为什么这个绑定似乎只是部分起作用?当可以同时选择大量 ListBoxItems 时,是否有更好的方法来处理 IsSelected 属性的绑定?

Why does this binding seem to be only partially working? Is there a better way to handle the binding of the IsSelected property when large numbers of ListBoxItems can be selected simultaneously?

此行为并非仅在使用 Ctrl+A 命令时才会发生 - 当使用 shift+click 选择所有项目时,我会得到相同的结果.

This behavior doesn't happen exclusively with the Ctrl+A command - I get the same results when selecting all the items using a shift+click.

推荐答案

我认为您看到的行为是由于 VirtualizingStackPanel.IsVirtualizing 导致的 True绑定到 ListBox

I think the behavior you're seeing is to due to VirtualizingStackPanel.IsVirtualizing which is True by default when binding to ItemsSource of ListBox

如果你设置你的 ListBox 比如:

if you for eg set your ListBox such as:

<ListBox VirtualizingStackPanel.IsVirtualizing="False" SelectionMode="Extended" ItemsSource="{Binding DataContext.ResultList, RelativeSource={RelativeSource AncestorType=Window}}">

<ListBox ...>
  ...
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <StackPanel />
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
</ListBox>

那么您应该会看到所有绑定项目的 IsSelected 相应地使用 Ctrl+A 或 Shift + ...

then you should see all your bound items have their IsSelected updated accordingly with Ctrl+A or Shift + ...

集合的 Count 等属性即使使用虚拟化也会报告正确的值,以适应诸如计算所需的 ScrollBar.Height 之类的事情.视口之外的项目不会被渲染,因此在它们真正被使用之前没有绑定对它们生效.

Properties such as Count of the collection even with virtualization would report the correct value to accommodate for things like computing the required ScrollBar.Height. Items which are outside the View-port do not get rendered hence no bindings are in effect on them until they actually get used.

这篇关于列表框“IsSelected"绑定仅部分工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆