I have the following ListView
which contains the following XAML definition:
<ListView Height="307" Width="991" Name="OrderListView" ItemsSource="{ Binding Orders }">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=isVisible}" Value="False">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="A header" DisplayMemberBinding="{Binding Path=StoreNumber}"></GridViewColumn>
...
</GridView>
</ListView.View>
</ListView>
And the model which is linked to data context:
public sealed class DDModel : INotifyPropertyChanged
{
public ObservableCollection<Order> Orders
{
get;
set;
}
public event PropertyChangedEventHandler PropertyChanged;
}
and Order has a boolean property isVisible
(see the trigger from style)
I create an event for text box at keyup:
private void OrderSearch_KeyUp(object sender, KeyEventArgs e)
{
_backup = this.DataContext as DDModel;
string keyword = this.OrderSearch.Text;
var query = _backup.Orders.ToList();
// make them all visible
query.ForEach(p => p.isVisible = true);
if (!string.IsNullOrWhiteSpace(keyword))
{
//then hide the items which doesn't match with keyword
query.Where(p => !p.OrderNumber.ToString().StartsWith(keyword)).ToList().ForEach(p => p.isVisible = false);
}
}
The ListView is not refreshed, the trigger doesn't work and some items are not hidden.
But _backup
contains few items which isVisible
is set to false;
Where is my mistake ?
PS: The Orders
is not null or empty, is loaded from database when window is initialised. The listview also is not empty, contains few records (rows).