Listbox “IsSelected” binding only partially working

前端 未结 1 554
独厮守ぢ
独厮守ぢ 2021-01-13 08:15

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

相关标签:
1条回答
  • 2021-01-13 09:17

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

    if you for eg set your ListBox such as:

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

    or

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

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

    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.

    0 讨论(0)
提交回复
热议问题