我有一个ListBox我通过绑定动态填充的(这是在 a 中定义的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>
每个ListBoxItem的IsSelected属性都绑定到IsSelected自定义对象上的属性。
当我选择单个ListBoxItems 时,绑定工作正常 - 自定义对象的IsSelected属性在我的 ViewModel 中更新。但是,如果我ListBoxItem使用 Ctrl+A 命令选择所有 s,则只有当前可见ListBoxItem的 s(当前在我的滚动视口中的那些)更新其 ViewModel 绑定。在前端,所有的ListBoxItems 似乎都被选中,ListBox.SelectedItems.Count容器上的属性ListBox显示所有项目都被选中。
此外,当我在使用 Ctrl+AListBox选择所有 s 后滚动浏览时,绑定会在每个滚动到视图中时成功更新。ListBoxItemListBoxItem
为什么这个绑定似乎只是部分起作用?当可以同时选择IsSelected大量时,是否有更好的方法来处理属性的绑定?ListBoxItems
编辑: 这种行为不仅仅发生在 Ctrl+A 命令中——当使用 shift+click 选择所有项目时,我得到相同的结果。