我有一个ListBox
我通过绑定动态填充(这是在DataTemplate
,这就是绑定有些不寻常的原因):
<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>
Each ListBoxItem
's IsSelected
属性绑定到IsSelected
自定义对象上的属性。
当我选择个人时ListBoxItem
是,绑定工作正常 - 自定义对象IsSelected
属性在我的 ViewModel 中更新。但是,如果我选择所有ListBoxItem
使用 Ctrl+A 命令,仅当前可见ListBoxItem
s(当前位于我的滚动视口中的那些)更新其 ViewModel 绑定。在前端,所有ListBoxItem
s 似乎已被选中,并且ListBox.SelectedItems.Count
容器上的属性ListBox
显示所有项目都被选中。
此外,当我滚动浏览ListBox
选择全部后ListBoxItem
使用 Ctrl+A 时,每次更新绑定都会成功ListBoxItem
已滚动到视图中。
为什么这个绑定似乎只部分起作用?有没有更好的方法来处理绑定IsSelected
当大量财产ListBoxItems
可以同时选择吗?
Edit:此行为不仅仅发生在使用 Ctrl+A 命令时 - 当使用 Shift+单击选择所有项目时,我得到相同的结果。
我认为你所看到的行为是由于VirtualizingStackPanel.IsVirtualizing
这是True
绑定时默认ItemsSource
of ListBox
如果你例如设置你的ListBox
例如:
<ListBox VirtualizingStackPanel.IsVirtualizing="False" SelectionMode="Extended" ItemsSource="{Binding DataContext.ResultList, RelativeSource={RelativeSource AncestorType=Window}}">
or
<ListBox ...>
...
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
那么你应该看到所有绑定的物品都有它们的IsSelected
使用 Ctrl+A 或 Shift + ... 进行相应更新
属性如Count
即使使用虚拟化,集合的也会报告正确的值以适应诸如计算所需的内容之类的事情ScrollBar.Height
。视口之外的项目不会被渲染,因此在实际使用它们之前,任何绑定都不会对其生效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)