Skip to content

Commit

Permalink
Fix #18: filter display is not persisted in sample control
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Jul 4, 2019
1 parent df0cfd4 commit 480d154
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Expand Up @@ -5,14 +5,14 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dgx="clr-namespace:DataGridExtensions;assembly=DataGridExtensions"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
d:DesignHeight="30" d:DesignWidth="300">
<Control.Template>
<ControlTemplate>
<Grid>
<Control Style="{DynamicResource {x:Static dgx:DataGridFilter.IconStyleKey}}"/>
<StackPanel x:Name="inputPanel" Orientation="Horizontal" Margin="4,0,2,0" Background="Red">
<TextBlock Text="> "/>
<TextBox x:Name="textBox" TextChanged="TextBox_TextChanged" MinWidth="20"/>
<TextBox x:Name="textBox" TextChanged="TextBox_TextChanged" MinWidth="20" />
</StackPanel>
</Grid>
<ControlTemplate.Triggers>
Expand Down
25 changes: 17 additions & 8 deletions DataGridExtensionsSample/IntegerGreatherThanFilterControl.xaml.cs
@@ -1,5 +1,6 @@
namespace DataGridExtensionsSample
{
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using DataGridExtensions;
Expand All @@ -16,13 +17,18 @@ public IntegerGreatherThanFilterControl()
InitializeComponent();
}

public override void OnApplyTemplate()
{
base.OnApplyTemplate();

_textBox = Template.FindName("textBox", this) as TextBox;
}

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
_textBox = (TextBox)sender;
var text = _textBox.Text;
int threshold;
var text = ((TextBox)sender)?.Text;

Filter = !int.TryParse(text, out threshold) ? null : new ContentFilter(threshold);
Filter = !int.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture, out var threshold) ? null : new ContentFilter(threshold);
}

public IContentFilter Filter
Expand All @@ -38,10 +44,11 @@ public IContentFilter Filter

private void Filter_Changed(object newValue)
{
if ((newValue == null) && (_textBox != null))
{
_textBox.Text = string.Empty;
}
var textBox = _textBox;
if (textBox == null)
return;

textBox.Text = (newValue as ContentFilter)?.Value ?? string.Empty;
}

class ContentFilter : IContentFilter
Expand All @@ -62,6 +69,8 @@ public bool IsMatch(object value)

return int.TryParse(value.ToString(), out i) && (i > _threshold);
}

public string Value => _threshold.ToString(CultureInfo.CurrentCulture);
}
}
}

0 comments on commit 480d154

Please sign in to comment.