【问题标题】:WPF DataGrid Filtering - CollectionViewSource RefreshingWPF DataGrid 过滤 - CollectionViewSource 刷新
【发布时间】:2014-04-15 10:30:36
【问题描述】:

我想知道如何在单击按钮时刷新 CollectionViewSource?

到目前为止我有

<Window.Resources>
    <CollectionViewSource x:Key="cvsCustomers"
                          Source="{Binding CustomerCollection}" 
                          Filter="CollectionViewSource_Filter" >
    </CollectionViewSource>
</Window.Resources>

这会创建 CollectionViewSource...

<DataGrid HorizontalAlignment="Left" 
              Height="210" 
              Margin="47,153,0,0"
              VerticalAlignment="Top" Width="410"
              ItemsSource="{Binding Source={StaticResource cvsCustomers}}"
              CanUserAddRows="False"

将源绑定到我的 Datagrid

    private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
    {
        Customer t = e.Item as Customer;
        if (t != null)
        // If filter is turned on, filter completed items.
        {
            if (t.Name.Contains(txtSearch.Text))
            {
                e.Accepted = true;
            }
            else
            {
                e.Accepted = false;
            }
        }
    }

我的视图中有一个过滤器,

一切似乎都在工作(项目被绑定到网格)但是我如何刷新视图或网格,以便我可以再次触发上述功能,以便网格确实被过滤? (通过真正的按钮点击)

谢谢

【问题讨论】:

  • 您的问题与我所遵循的示例略有不同,当我将您的尝试与我的努力进行比较时,这让我明白了为什么我的观点没有令人耳目一新。

标签: c# wpf xaml datagrid collectionviewsource


【解决方案1】:

在 CollectionViewSource 的 View 属性上调用 Refresh() 以刷新它。

如果您想在单击按钮时执行此操作,则需要先从窗口资源访问 CollectionViewSource,然后对其视图调用刷新。

((CollectionViewSource)this.Resources["cvsCustomers"]).View.Refresh();

【讨论】:

  • 从 .Net 4.5 开始,您可以将 IsLiveGroupingRequested="True" IsLiveSortingRequested="True" 属性添加到 XAML CollectionViewSource 定义以自动刷新 stackoverflow.com/questions/41515053/…
  • @Chashitsu 那么它如何知道我的过滤器 TextBox 发生了变化?什么魔法让它知道 TextBox 的变化,所以它可以自动刷新?
猜你喜欢
  • 2010-10-14
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 2012-12-09
  • 2013-11-06
  • 2010-12-19
  • 2010-10-30
  • 1970-01-01
相关资源
最近更新 更多