开发者

ListView selection problem - Caching prevents selection of many items

开发者 https://www.devze.com 2023-01-18 21:05 出处:网络
I have a problem with the way a ListView selects its items. I have a ListView bound to an ObservableCollection of items which have a \'Selected\' property. I have bound the selection state like this:

I have a problem with the way a ListView selects its items.

I have a ListView bound to an ObservableCollection of items which have a 'Selected' property. I have bound the selection state like this:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="IsSelected" Value="{Binding Path=Selected, Mode=TwoWay}" />
    </Style>
</ListView.ItemContainerStyle>

But with approxemitly 1000 Items in the list, the ListView only holds containers for the visible objects on start or about 500 when already scrolled through. So even if i select all (Ctrl+A), the selection gets only set for these 500.

I also tried a lot of other solutions that came to my mind, but none wor开发者_如何学Pythonked.

  • ListView.SelectedItems cant be bound to.
  • Using SelectionChanged event to set 'Selected' property - Fired a lot of SelectionChanged events when scrolling to an area, that was not cached at selection time and therefore the items were 'new selected' for the list.
  • Using SelectionChanged event of the ListView to add and remove the changed selection to an extra collection - Could not select from ViewModel, since works only in one direction.

Is it anyhow possible to have a ListView where I can select more than 500 Items and have this info somewhere in the ViewModel. It should also be possible to select/deselect from ViewModel.

Thanks


I would recommend that you use a data template for your listviewitems and in that datatemplate you should have a check button, or check box etc (ie a control that maintains state). I would then databind that to your viewmodel. I would then make it so that you bypass your selected mechanism entirely on your listview control. You can make templates to them your listviewitems based on the button state which is bindable to your viewmodel.

This solution I am proposing isn't wonderful, as compared to IsSelected working as you would expect it to. If you wanted a tidier version, perhaps you can implement what I am suggesting in as a new encapsulated control which makes IsSelected observable/bindable as you would expect it to be. Possibly the third party controls that are available might support this behavior?

0

精彩评论

暂无评论...
验证码 换一张
取 消