I have two List boxes ,both has item source set as MyList.In listbox1 I want to filter the entries based on some predicate.I achieve that by following code

ICollectionView listview= CollectionViewSource.GetDefaultView(MyList);

listview.filter(predicate)

My problem is I dont want this filtering to be applied to listbox2 but it is important for me to keep one itemsource.How can I achieve this.

  <ListBox Name="listbox1" ItemsSource="{Binding MyList}"/>
    <ListBox Name="listbox2" ItemsSource="{Binding MyList}"/>

I tried setting VirtualizingStackpanel.IsVirtualizing to false ,but it doesnt work.

有帮助吗?

解决方案

Instead of binding the source collection to the list boxes, you can create a collection view source where you need filtering and bind its view to the list box.

public CollectionViewSource ViewSource1 { get; set; }


ViewSource1 = new CollectionViewSource { Source = MyList};
ViewSource1.View.Filter(predicate1)

Then bind the View of the view source to the first list box. You can bind the source collection to the second list box.

 <ListBox Name="listbox1" ItemsSource="{Binding ViewSource1.View}"/>
 <ListBox Name="listbox2" ItemsSource="{Binding MyList}"/>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top