Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I suggest you do the following in your Presenter:</p> <p>=> Create a ListCollectionView field. Set it to be equal to the "Default Collection View" of your collection, and use it as the ItemsSource for the list control. Something like:</p> <pre><code> public class Presenter() { private ListCollectionView lcv; public Presenter() { this.lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(animalsCollection); listControl.ItemsSource = this.lcv; } } </code></pre> <p>=> In the handler/logic for the RadioButton's corresponding event, filter the ListCollectionView. Something like:</p> <p><pre><code> void OnCheckedChanged() { bool showMammals = radioButton.IsChecked; this.lcv.Filter = new Predicate((p) => (p as Animal).IsMammal == showMammals); this.lcv.Refresh(); } </pre></code></p> <p>Hope this helps.</p> <p><strong>EDIT:</strong></p> <p>While doing this is possible using MVP, using MVVM should be a better choice, IMHO (and as mentioned by the other answer). To help you out, I wrote a sample that implements your requirements via MVVM. See below:</p> <p>XAML:</p> <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.DataContext&gt; &lt;local:ViewModel/&gt; &lt;/Window.DataContext&gt; &lt;Grid&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;RadioButton x:Name="rb1" GroupName="MyGroup" Content="IsMammal = true" Checked="rb1_Checked"/&gt; &lt;RadioButton x:Name="rb2" GroupName="MyGroup" Content="IsMammal = false" Checked="rb2_Checked"/&gt; &lt;ListBox ItemsSource="{Binding Path=AnimalsCollectionView}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Path=Name}"/&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Code-behind:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Collections.ObjectModel; using System.Threading; using System.ComponentModel; namespace WpfApplication1 { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void rb1_Checked(object sender, RoutedEventArgs e) { (this.DataContext as ViewModel).UpdateFilter(true); } private void rb2_Checked(object sender, RoutedEventArgs e) { (this.DataContext as ViewModel).UpdateFilter(false); } } public class ViewModel : INotifyPropertyChanged { private ObservableCollection&lt;Animal&gt; animals = new ObservableCollection&lt;Animal&gt;(); private ListCollectionView animalsCollectionView; public ListCollectionView AnimalsCollectionView { get { return this.animalsCollectionView; } } public void UpdateFilter(bool showMammals) { this.animalsCollectionView.Filter = new Predicate&lt;object&gt;((p) =&gt; (p as Animal).IsMammal == showMammals); this.animalsCollectionView.Refresh(); } public ViewModel() { this.animals.Add(new Animal() { Name = "Dog", IsMammal = true }); this.animals.Add(new Animal() { Name = "Cat", IsMammal = true }); this.animals.Add(new Animal() { Name = "Bird", IsMammal = false }); this.animalsCollectionView = (ListCollectionView)CollectionViewSource.GetDefaultView(this.animals); } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } #endregion } public class Animal : INotifyPropertyChanged { private string name; public string Name { get { return this.name; } set { this.name = value; this.OnPropertyChanged("Name"); } } private bool isMammal; public bool IsMammal { get { return this.isMammal; } set { this.isMammal = value; this.OnPropertyChanged("IsMammal"); } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } #endregion } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload