Skip to content

Commit d66b464

Browse files
committed
Fix: MultipleChoiceFilter looses selection when control gets unloaded.
1 parent 37c0c90 commit d66b464

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

src/DataGridExtensions/MultipleChoiceFilter.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ public MultipleChoiceFilter()
4040
/// <summary>
4141
/// Gets or sets the filter.
4242
/// </summary>
43-
public MultipleChoiceContentFilter? Filter
43+
public IMultipleChoiceContentFilter? Filter
4444
{
45-
get => (MultipleChoiceContentFilter)GetValue(FilterProperty);
45+
get => (IMultipleChoiceContentFilter)GetValue(FilterProperty);
4646
set => SetValue(FilterProperty, value);
4747
}
4848
/// <summary>
4949
/// The filter property
5050
/// </summary>
5151
public static readonly DependencyProperty FilterProperty =
52-
DependencyProperty.Register("Filter", typeof(MultipleChoiceContentFilter), typeof(MultipleChoiceFilter), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (sender, _) => ((MultipleChoiceFilter)sender).Filter_Changed()));
52+
DependencyProperty.Register("Filter", typeof(IMultipleChoiceContentFilter), typeof(MultipleChoiceFilter), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (sender, _) => ((MultipleChoiceFilter)sender).Filter_Changed()));
5353

5454
/// <summary>
5555
/// Gets or sets a value that controls if the optional text filter is visible.
@@ -116,7 +116,7 @@ public string? Text
116116
/// Defines the Text property.
117117
/// </summary>
118118
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
119-
"Text", typeof(string), typeof(MultipleChoiceFilter), new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (sender, e) => ((MultipleChoiceFilter)sender).Text_Changed()));
119+
"Text", typeof(string), typeof(MultipleChoiceFilter), new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (sender, _) => ((MultipleChoiceFilter)sender).Text_Changed()));
120120

121121
private void Text_Changed()
122122
{
@@ -188,7 +188,7 @@ public override void OnApplyTemplate()
188188
/// <param name="items">The items to filter.</param>
189189
/// <param name="text">The optional text to pre-filter the items.</param>
190190
/// <returns>The filter.</returns>
191-
protected virtual MultipleChoiceContentFilter CreateFilter(IEnumerable<string?>? items, string? text = null)
191+
protected virtual IMultipleChoiceContentFilter CreateFilter(IEnumerable<string?>? items, string? text = null)
192192
{
193193
return new MultipleChoiceContentFilter(items, text);
194194
}
@@ -264,6 +264,9 @@ private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e
264264
{
265265
var listBox = (ListBox)sender;
266266

267+
if (!listBox.IsLoaded)
268+
return;
269+
267270
var selectedItems = listBox.SelectedItems.Cast<string>().ToArray();
268271

269272
var areAllItemsSelected = listBox.Items.Count == selectedItems.Length;
@@ -279,9 +282,30 @@ private void UpdateSourceValuesTarget()
279282
}
280283

281284
/// <summary>
282-
/// Base class for a multiple choice content filter
285+
/// Interface for a multiple choice content filter
286+
/// </summary>
287+
public interface IMultipleChoiceContentFilter : IContentFilter
288+
{
289+
/// <summary>
290+
/// Gets the items to filter.
291+
/// </summary>
292+
ICollection<string?>? Items { get; }
293+
294+
/// <summary>
295+
/// Gets the text to filter.
296+
/// </summary>
297+
string? Text { get; }
298+
299+
/// <summary>
300+
/// Gets the text regex to filter.
301+
/// </summary>
302+
Regex? Regex { get; }
303+
}
304+
305+
/// <summary>
306+
/// Default implementation for a multiple choice content filter
283307
/// </summary>
284-
public class MultipleChoiceContentFilter : IContentFilter
308+
public class MultipleChoiceContentFilter : IMultipleChoiceContentFilter
285309
{
286310
/// <summary>
287311
/// Initializes a new instance of the <see cref="MultipleChoiceContentFilter"/> class.

src/DataGridExtensionsSample/Views/Customized2View.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace DataGridExtensionsSample.Views
22
{
3+
using System.Composition;
34
using System.Windows.Data;
45

56
using DataGridExtensions;
@@ -10,6 +11,7 @@
1011
/// Interaction logic for Customized2View.xaml
1112
/// </summary>
1213
[DataTemplate(typeof(Customized2ViewModel))]
14+
[Shared]
1315
public partial class Customized2View
1416
{
1517
public Customized2View()

0 commit comments

Comments
 (0)