Skip to content

Commit acf608b

Browse files
committed
Fix #81: "GetFilter().Clear" doesn't clear the Listbox of the MultipleChoiceFilter
1 parent 4c98383 commit acf608b

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

src/DataGridExtensions/MultipleChoiceFilter.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ public override void OnApplyTemplate()
154154
var listBox = _listBox = Template?.FindName("PART_ListBox", this) as ListBox;
155155
if (listBox == null)
156156
return;
157+
157158
listBox.SelectionChanged += ListBox_SelectionChanged;
159+
listBox.Loaded += ListBox_Loaded;
158160

159161
var filterColumnControl = this.FindAncestorOrSelf<DataGridFilterColumnControl>();
160162

@@ -170,15 +172,7 @@ public override void OnApplyTemplate()
170172

171173
_dataGrid.SetTrackFocusedCell(true);
172174

173-
var filter = Filter;
174-
175-
if (filter?.Items == null && listBox.IsLoaded)
176-
{
177-
listBox.SelectAll();
178-
}
179-
180175
var items = (INotifyCollectionChanged)listBox.Items;
181-
182176
items.CollectionChanged += ListBox_ItemsCollectionChanged;
183177
}
184178

@@ -235,6 +229,11 @@ private void ListBox_ItemsCollectionChanged(object? sender, NotifyCollectionChan
235229
}
236230
}
237231

232+
private void ListBox_Loaded(object sender, RoutedEventArgs e)
233+
{
234+
Filter_Changed();
235+
}
236+
238237
private void Filter_Changed()
239238
{
240239
var listBox = _listBox;
@@ -247,10 +246,7 @@ private void Filter_Changed()
247246

248247
if (filter?.Items == null)
249248
{
250-
if (listBox.IsLoaded)
251-
{
252-
listBox.SelectAll();
253-
}
249+
listBox.SelectAll();
254250
return;
255251
}
256252

src/DataGridExtensionsSample/DataGridExtensionsSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
22
<PropertyGroup>
3-
<TargetFrameworks>net5.0-windows7.0</TargetFrameworks>
3+
<TargetFrameworks>net6.0-windows7.0</TargetFrameworks>
44
<UseWPF>true</UseWPF>
55
<OutputType>WinExe</OutputType>
66
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>

src/DataGridExtensionsSample/Views/Customized2View.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
<Decorator Width="10" />
7171
<Button x:Name="OpenAndPopulateAFilterButton" VerticalAlignment="Top" Padding="5,2" Command="{Binding OpenAndPopulateAFilterCommand}">Open &amp; Populate a Filter</Button>
7272
<Decorator Width="10" />
73+
<Button x:Name="ClearFilter" VerticalAlignment="Top" Padding="5,2" Command="{Binding ClearFilterCommand}" CommandParameter="{Binding ElementName=DemoDataGrid}">Clear Filter</Button>
74+
<Decorator Width="10" />
7375
<Button x:Name="ProgrammaticAccessToFilterControlButton" VerticalAlignment="Top" Padding="5,2" Command="{Binding ProgrammaticAccessToFilterControlCommand}">Change Popup Caption</Button>
7476
</StackPanel>
7577
<StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" HorizontalAlignment="Right">

src/DataGridExtensionsSample/Views/Customized2ViewModel.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Collections.ObjectModel;
66
using System.ComponentModel;
7+
using System.Windows.Controls;
78
using System.Windows.Input;
89

910
using DataGridExtensions;
@@ -42,6 +43,14 @@ public Customized2ViewModel(DataProvider dataProvider)
4243
public Predicate<object> GlobalFilter { get; } = item => (item as DataItem)?.Column1?.Contains('7') ?? false;
4344
#pragma warning restore CA1307 // Specify StringComparison for clarity
4445

46+
47+
public ICommand ClearFilterCommand => new DelegateCommand<DataGrid>(ClearFilter);
48+
49+
private void ClearFilter(DataGrid? dataGrid)
50+
{
51+
dataGrid?.GetFilter().Clear();
52+
}
53+
4554
public ICommand ClearIpsumCommand => new DelegateCommand(ClearIpsum);
4655

4756
private void ClearIpsum()

0 commit comments

Comments
 (0)