1

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.

  1. Do I need to refresh explicitly in the event handler at all?
  2. Do I need to add another (kind of) binding?
  3. Do I need to implement anything more in the view model?

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" .../>

Edit

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));
  }
}

1 Answer 1

1

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}"/>
Sign up to request clarification or add additional context in comments.

12 Comments

Do you have a link to some kind of documentation of it? Are there any others way to work-around it? Is there any chance that it's intended behavior and that we just misuse it? Also, I notice that you omitted the part referring to the view model instance through DataContext. Is that possible? I always go through that, cast and then refer to it, which is inconvenient.
MSDN : "Re-creates the view. "
I just crossed paths with this issue multiple times , i would love to find out i'm mistaken .
You might actually be onto something, so +1. When I set the filter to null in my event handler, I get the difference in the data set shown. However, it seems not to follow the selection of the check boxes. Suggestions?
Yes that's a different question . Checked and Unchecked are different events in WPF CheckBox
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.