I'm trying to add a Filter to my ListView. The filter depends on an input from a TextBox. I'm adding a filter to the ListView and trigger a Refresh if the TextBox Input is changed. My Problem: The Filter Method is only called once.
What I have:
I got a ListView in a XAML file.
<TextBox Grid.Row="0" x:Name="textBoxGroupLayers" FontSize="14" TextChanged="textBoxGroupLayers_TextChanged"/>
<ListView Grid.Row="1" x:Name="listViewGroupLayers" FontSize="14" />
I'm adding Items to the ListView via
listViewGroupLayers.Items.Add(itemToAdd);
I'm adding the Filter:
this.listViewGroupLayers.Items.Filter = UserFilter;
Notice: listViewGroupLayers is a ListView and listViewGroupLayers.Items is an ItemCollection.
My Filter:
private bool UserFilter(object item)
{
if (String.IsNullOrEmpty(this.textBoxGroupLayers.Text))
{
return true;
}
else
{
MyObject myObject = (item as ListViewItem).Tag as MyObject;
if (myObject == null)
{
return true;
}
bool itemShouldBeVisible = myObject.Name.IndexOf(this.textBoxGroupLayers.Text, StringComparison.OrdinalIgnoreCase) >= 0;
return itemShouldBeVisible;
}
}
The Refresh:
private void textBoxGroupLayers_TextChanged(object sender, TextChangedEventArgs e)
{
this.listViewGroupLayers.Items.Refresh();
}
Now happens the following: I setted a Breakpoint in the UserFilter. If I type the first letter in the TextBox, the Breakpoint is gonna be active and the filter is working fine. But if I type the second letter now, the filter isn't called.
If I just do the following, everything works fine:
private void textBoxGroupLayers_TextChanged(object sender, TextChangedEventArgs e)
{
this.listViewGroupLayers.Items.Filter = UserFilter;
}
But this seems pretty dirty to me.
My question:
Why isn't the filter working after the first time called?
I checked the instance of this.listViewGroupLayers.Items and it always has the filter object.