Skip to content

Commit

Permalink
Fix #65: Calling DataGrid.GetFilter() from code turns filter controls…
Browse files Browse the repository at this point in the history
… visible, even though IsAutoFilterEnabled is false.
  • Loading branch information
tom-englert committed Jun 20, 2021
1 parent 7372826 commit 9c08d15
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
21 changes: 19 additions & 2 deletions src/DataGridExtensions/DataGridFilterColumnControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using DataGridExtensions.Framework;

using Throttle;
using TomsToolbox.Wpf.Converters;
using BooleanToVisibilityConverter = TomsToolbox.Wpf.Converters.BooleanToVisibilityConverter;

/// <summary>
/// This class is the control hosting all information needed for filtering of one column.
Expand All @@ -24,7 +26,6 @@
/// <seealso cref="INotifyPropertyChanged" />
public class DataGridFilterColumnControl : Control, INotifyPropertyChanged
{
private static readonly BooleanToVisibilityConverter _booleanToVisibilityConverter = new BooleanToVisibilityConverter();
private static readonly ControlTemplate _emptyControlTemplate = new ControlTemplate();

private bool _getCellContentMustUseBinding;
Expand Down Expand Up @@ -74,7 +75,23 @@ private void Self_Loaded(object sender, RoutedEventArgs e)
// Bind our IsFilterVisible and Template properties to the corresponding properties attached to the
// DataGridColumnHeader.Column property. Use binding instead of simple assignment since columnHeader.Column is still null at this point.
var isFilterVisiblePropertyPath = new PropertyPath("(0)", DataGridFilterColumn.IsFilterVisibleProperty);
BindingOperations.SetBinding(this, VisibilityProperty, new Binding() { Path = isFilterVisiblePropertyPath, Source = column, Mode = BindingMode.OneWay, Converter = _booleanToVisibilityConverter });
var isAutoFilterEnabledPropertyPath = new PropertyPath("(0)", DataGridFilter.IsAutoFilterEnabledProperty);

BindingOperations.SetBinding(this, VisibilityProperty, new MultiBinding()
{
Converter = new CompositeMultiValueConverter()
{
MultiValueConverter = LogicalMultiValueConverter.And,
Converters = {
TomsToolbox.Wpf.Converters.BooleanToVisibilityConverter.Default
}
},
Bindings =
{
new Binding() { Path = isFilterVisiblePropertyPath, Source = column, Mode = BindingMode.OneWay },
new Binding() { Path = isAutoFilterEnabledPropertyPath, Source = DataGrid, Mode = BindingMode.OneWay }
}
}); ;

var templatePropertyPath = new PropertyPath("(0)", DataGridFilterColumn.TemplateProperty);
BindingOperations.SetBinding(this, TemplateProperty, new Binding() { Path = templatePropertyPath, Source = column, Mode = BindingMode.OneWay });
Expand Down
12 changes: 6 additions & 6 deletions src/DataGridExtensions/DataGridFilterHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ internal void Enable(bool value)
{
_isFilteringEnabled = value;

var visibility = value ? Visibility.Visible : Visibility.Hidden;

foreach (var control in FilterColumnControls)
if (!value)
{
control.Visibility = visibility;
Clear();
}
else
{
EvaluateFilter();
}

EvaluateFilter();
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/DataGridExtensionsSample/Views/BasicView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public BasicView()
// Sample to manipulate the filter values by code.
DataGrid.Columns[0].SetFilter("True");
DataGrid.Columns[2].SetFilter("3");
DataGrid.Columns[3].SetIsFilterVisible(false);
}));

// Sample usage of the filtering event
Expand Down

0 comments on commit 9c08d15

Please sign in to comment.