Reputation: 39058
I've bound my data grid and a bunch of check boxes to the following properties in my view model (typed Presenter).
public ListCollectionView AllOrdersView { get; set; }
public IEnumerable<CarrierType> AllCarrierTypes
{
get { return _allCarrierTypes; }
set
{
_allCarrierTypes = value;
OnPropertyChanged();
}
}
In the constructor of it, I assign a filter like so.
public Presenter()
{
_allOrders = new ObservableCollection<Order>(DataAccessor.GetOrders());
AllOrdersView = new ListCollectionView(_allOrders);
AllOrdersView.Filter = element => AllCarrierTypes
.Where(x => x.Active).Contains(((Order)element).CarrierType);
}
The aim is to filter off the orders that have a carrier of type that's unchecked. At the moment, it seems that the filtration only takes place initially, as the constructor is invoked. My hope was that, since the check boxes are bound, I need no further intervention with the code. It's not the case. As I select/deselect the check boxes, the data grid stays unaffected.
Furthermore, suspecting that it's got to do with the need to refresh the view, I added a call to the view as follows.
private void ToggleButton_OnUnchecked(Object sender, RoutedEventArgs eventArgs)
{
((Presenter)DataContext).AllOrdersView.Refresh();
}
Sadly, that doesn't seem to make any difference and I'm all stuck. So, my questions are these.
My bindings are done in the following way.
<ListBox ItemsSource="{Binding AllCarrierTypes}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}"
IsChecked="{Binding Active,Mode=TwoWay}"
Checked="ToggleButton_OnChecked"
Unchecked="ToggleButton_OnUnchecked"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<DataGrid x:Name="dataGrid"
ItemsSource="{Binding AllOrdersView}"
AutoGeneratingColumn="DataGrid_OnAutoGeneratingColumn" .../>
Per requestion - full version of the CarrierType class in the model.
public class CarrierType : INotifyPropertyChanged
{
private bool _active;
public int Id { get; set; }
public String Name { get; set; }
public bool Active
{
get { return _active; }
set
{
_active = value;
OnPropertyChanged();
}
}
public override String ToString()
{
return Name;
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Upvotes: 1
Views: 280
Reputation: 12533
There's seems to be a problem with the ListCollectionView in .NET 4.5 (not sure if the problem existed earlier). You would need to call Refresh() but it doesn't seem to work.
What I do is to re-assign the filter.
private void ToggleButton_OnUnchecked(Object sender, RoutedEventArgs eventArgs)
{
AllOrdersView.Filter = element => AllCarrierTypes
.Where(x => x.Active).Contains(((Order)element).CarrierType);
}
As requested in the comments below :
<CheckBox IsChecked="{Binding Active,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
Upvotes: 1