@fatih uyanık, Welcome to Microsoft Q&A, based on my test, I also reproduced the same problem with you when I build the ObservableDictionary class.
I recommend that you use the ViewModel Method to do it,
Here is a code example you could refer to.
ViewModel class:
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
public class MainViewModel : INotifyPropertyChanged
{
// Sample data collection
private ObservableCollection<DataItem> _originalItems = new ObservableCollection<DataItem>
{
new DataItem { Name = "Apple", Category = "Fruit", IsActive = true },
new DataItem { Name = "Carrot", Category = "Vegetable", IsActive = true },
new DataItem { Name = "Banana", Category = "Fruit", IsActive = false },
new DataItem { Name = "Broccoli", Category = "Vegetable", IsActive = true }
};
private ObservableCollection<DataItem> _filteredItems;
public ObservableCollection<DataItem> FilteredItems
{
get => _filteredItems;
set
{
_filteredItems = value;
OnPropertyChanged();
}
}
// Filter properties
private bool _showAll = true;
public bool ShowAll
{
get => _showAll;
set
{
if(_showAll != value)
{
_showAll = value;
if(value)
{
ShowFruits = false;
ShowVegetables = false;
}
OnPropertyChanged();
ApplyFilters();
}
}
}
private bool _showFruits;
public bool ShowFruits
{
get => _showFruits;
set
{
if(_showFruits != value)
{
_showFruits = value;
if(value)
{
ShowAll = false;
ShowVegetables = false;
}
OnPropertyChanged();
ApplyFilters();
}
}
}
private bool _showVegetables;
public bool ShowVegetables
{
get => _showVegetables;
set
{
if(_showVegetables != value)
{
_showVegetables = value;
if(value)
{
ShowAll = false;
ShowFruits = false;
}
OnPropertyChanged();
ApplyFilters();
}
}
}
private bool _showActiveOnly;
public bool ShowActiveOnly
{
get => _showActiveOnly;
set
{
if(_showActiveOnly != value)
{
_showActiveOnly = value;
OnPropertyChanged();
ApplyFilters();
}
}
}
public MainViewModel()
{
FilteredItems = new ObservableCollection<DataItem>( _originalItems );
}
private void ApplyFilters()
{
var filtered = _originalItems.AsEnumerable();
// Apply category filter
if(!ShowAll)
{
if(ShowFruits)
{
filtered = filtered.Where( item => item.Category == "Fruit" ); // Fixed typo
}
else if(ShowVegetables)
{
filtered = filtered.Where( item => item.Category == "Vegetable" );
}
}
// Apply active filter
if(ShowActiveOnly)
{
filtered = filtered.Where( item => item.IsActive );
}
FilteredItems.Clear();
foreach(var item in filtered)
{
FilteredItems.Add( item );
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
}
private ICommand _applyFiltersCommand;
public ICommand ApplyFiltersCommand => _applyFiltersCommand ??= new RelayCommand( ApplyFilters );
}
// Simple RelayCommand implementation
public class RelayCommand : ICommand
{
private readonly Action _execute;
public RelayCommand(Action execute)
{
_execute = execute;
}
public bool CanExecute(object parameter) => true;
public void Execute(object parameter)
{
_execute();
}
public event EventHandler CanExecuteChanged;
}
public class DataItem
{
public string Name { get; set; }
public string Category { get; set; }
public bool IsActive { get; set; }
}
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}
XAML:
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Filter Controls -->
<GroupBox Grid.Row="0" Header="Category Filters" Margin="0,0,0,10">
<StackPanel Orientation="Horizontal">
<RadioButton GroupName="CategoryGroup" Content="All"
IsChecked="{Binding ShowAll, Mode=TwoWay}" Margin="5">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Setter Property="Command" Value="{Binding ApplyFiltersCommand}"/>
</Style>
</RadioButton.Style>
</RadioButton>
<RadioButton GroupName="CategoryGroup" Content="Fruits"
IsChecked="{Binding ShowFruits, Mode=TwoWay}" Margin="5">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Setter Property="Command" Value="{Binding ApplyFiltersCommand}"/>
</Style>
</RadioButton.Style>
</RadioButton>
<RadioButton GroupName="CategoryGroup" Content="Vegetables"
IsChecked="{Binding ShowVegetables, Mode=TwoWay}" Margin="5">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Setter Property="Command" Value="{Binding ApplyFiltersCommand}"/>
</Style>
</RadioButton.Style>
</RadioButton>
</StackPanel>
</GroupBox>
<!-- Additional Filter -->
<CheckBox Grid.Row="0" Content="Show Active Items Only"
IsChecked="{Binding ShowActiveOnly, Mode=TwoWay}"
Margin="0,0,0,10" VerticalAlignment="Bottom"/>
<!-- Data Display -->
<DataGrid Grid.Row="1" ItemsSource="{Binding FilteredItems}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*"/>
<DataGridTextColumn Header="Category" Binding="{Binding Category}" Width="*"/>
<DataGridCheckBoxColumn Header="Active" Binding="{Binding IsActive}" Width="Auto"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
Tested Result:
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.