当绑定数据更改时,Gridview不会更新 [英] Gridview doesn't update when the bound data changes

查看:100
本文介绍了当绑定数据更改时,Gridview不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将我的数据网格和一堆复选框绑定到视图模型中的以下属性(键入演示者)。

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. 我需要在事件处理程序中显式刷新吗?


  2. 我需要在视图模型中实现更多的功能吗?

我的绑定以以下方式完成。

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



编辑



每个请求 - 完整版的模型中的 CarrierType 类。

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


推荐答案

似乎是.NET 4.5中的 ListCollectionView 的问题(不知道问题是否存在于前面)。您需要调用 Refresh(),但似乎不起作用。

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

根据以下评论的要求:

  <CheckBox IsChecked="{Binding Active,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

这篇关于当绑定数据更改时,Gridview不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆