0

I have a listBox with 1000+ items.

in xaml

<ListBox 
//some code here
SelectionChanged="OnSelectionChanged">
    <ListBox.ItemTemplate>
         <DataTemplate>
             <controls:MyCustomItem/>
         </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

In cs

 private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var item = lb.SelectedItem as MyCustomItem;
        var vm = DataContext as ViewModel;
        if (vm == null) return;
        foreach (var it in vm.ItemsForBinding)
        {
            it.IsSelected = false;
        }
        item .IsSelected = true;
    }

in MyCustomItem xaml

<UserControl
//Some code here
Style="{Binding Path=IsSelected, Converter = {StaticResource BoolToStyle}}">
    <Border 
    </Border>
</UserControl>

Where IsSelected - one of property of my ViewModel. Converter return one of two style (first if item selected, second - if no)

Always work, but I know - it's very resource-intensive, and the wrong decision. How to do it right?

jimpanzer
  • 3,470
  • 4
  • 44
  • 84
  • In point of view, the major problem is that you have a 1000+ items in a listbox. Do you really need that many items in a listbox. Can you break this down to multiple listboxes ? – Ali Khalid Apr 03 '13 at 07:24
  • No, I really need to show all items with filter function on single ListBox. I found solution and post it. – jimpanzer Apr 03 '13 at 08:10

1 Answers1

0

This post help me! Just override default ItemContainerStyle for my ListBox.

I removed:

metod OnSelectionChanged, UserControl MyCustomItem, converter for styles and

<ListBox.ItemTemplate>
     <DataTemplate>
         <controls:MyCustomItem/>
     </DataTemplate>
</ListBox.ItemTemplate>

from xaml.

Community
  • 1
  • 1
jimpanzer
  • 3,470
  • 4
  • 44
  • 84