Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1036347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:41:21+00:00 2026-05-16T14:41:21+00:00

I am playing with a sample WPF application that is tiered in a Model-View-Presenter

  • 0

I am playing with a sample WPF application that is tiered in a Model-View-Presenter manner. The Model is a collection of Animals which is displayed in a view via binding to properties in a presenter class. The XAML has an items control that displays all the animals in a model.

The model class has a boolean attribute called ‘IsMammal’. I want to introduce a filter in the XAML in the form of a radio button group that filters the collection of animals based on the ‘IsMammal’ attribute. Selection of the radiobutton ‘Mammals’ updates the items control with all the Animals that have the ‘IsMammal’ value set to true and when the value is toggled to ‘Non-Mammals’, the display is updated with all animals that have that particular boolean set to false. The logic to do the filtering is extremely simple. What is troubling me is the placement of the logic. I don’t want any logic embedded in the *.xaml.cs. I want the toggling of the radiobutton to trigger a logic in the presenter that tapers my animal collection to be rebound to the display.

Some guidance here will be extremely appreciated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:41:22+00:00Added an answer on May 16, 2026 at 2:41 pm

    I suggest you do the following in your Presenter:

    => 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:

    
        public class Presenter()
        {
            private ListCollectionView lcv;
    
            public Presenter()
            {
                this.lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(animalsCollection);
                listControl.ItemsSource = this.lcv;
            }
        }
    

    => In the handler/logic for the RadioButton’s corresponding event, filter the ListCollectionView. Something like:

    
    void OnCheckedChanged()
    {
         bool showMammals = radioButton.IsChecked;
         this.lcv.Filter = new Predicate((p) => (p as Animal).IsMammal == showMammals);
         this.lcv.Refresh();
    }
    

    Hope this helps.

    EDIT:

    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:

    XAML:

    <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">
        <Window.DataContext>
            <local:ViewModel/>
        </Window.DataContext>
        <Grid>
    
            <StackPanel Orientation="Vertical">
    
                <RadioButton x:Name="rb1" GroupName="MyGroup" Content="IsMammal = true" Checked="rb1_Checked"/>
                <RadioButton x:Name="rb2" GroupName="MyGroup" Content="IsMammal = false" Checked="rb2_Checked"/>
    
                <ListBox ItemsSource="{Binding Path=AnimalsCollectionView}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
    
        </Grid>
    </Window>
    

    Code-behind:

    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
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        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<Animal> animals = new ObservableCollection<Animal>();
            private ListCollectionView animalsCollectionView;
    
            public ListCollectionView AnimalsCollectionView
            {
                get { return this.animalsCollectionView; }
            }
    
            public void UpdateFilter(bool showMammals)
            {
                this.animalsCollectionView.Filter = new Predicate<object>((p) => (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
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.