【问题标题】:wpf datagrid refresh without INotifiedChanges没有 INotifiedChanges 的 wpf 数据网格刷新
【发布时间】:2012-05-22 14:34:20
【问题描述】:

我在 wpf 中有数据网格,在 .xaml.cs 中有一些代码

List<TaskHeader> taskHeaders;
//initialization of taskHeaders
taskDataGrid.ItemsSource = taskHeaders;

因此,在单击刷新按钮后,我需要将我的 taskHeaders 更改更新为 taskDataGrid 视图,但如果不实现 ObservableCollection,我就找不到方法。 taskDataGrid.Items.Refresh(); 不工作。

taskDataGrid.ItemsSource = null;
taskDataGrid.ItemsSource = taskHeaders;
taskDataGrid.Items.Refresh();

也不行 任何的想法?请帮忙

【问题讨论】:

  • 您为什么不想使用真正有效的东西?

标签: wpf datagrid refresh


【解决方案1】:

试试

CollectionViewSource.GetDefaultView(taskHeaders).Refresh();

【讨论】:

    【解决方案2】:

    我已经对此进行了测试,并且可行:

    我的 XAML:

    <Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <Button DockPanel.Dock="Bottom" Content="Change list and refresh grid" Click="OnRefreshButtonClicked"/>
        <DataGrid x:Name="taskDataGrid"/>
    </DockPanel>
    

    我的代码:

    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Data;
    
    namespace WpfApplication
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                var taskHeaders = new List<TaskHeader>();
                for (int i = 0; i < 10; ++i)
                    taskHeaders.Add(new TaskHeader() { Property = "Property " + i });
    
                this.taskDataGrid.ItemsSource = taskHeaders;
            }
    
            private void OnRefreshButtonClicked(object sender, RoutedEventArgs e)
            {
                var taskHeaders = (List<TaskHeader>)this.taskDataGrid.ItemsSource;
    
                // Make changes on taskHeaders by removing first item.
                taskHeaders.RemoveAt(0);
    
                CollectionViewSource.GetDefaultView(taskHeaders).Refresh();
            }
        }
    }
    

    还有我的虚拟 TaskHeader 类:

    namespace WpfApplication
    {
        public class TaskHeader
        {
            public string Property { get; set; }
        }
    }
    

    【讨论】:

    • 谢谢 Stipo,您选择我的方式来理解按钮单击事件或其他用户操作是刷新 DataGrid 所必需的,这里是所有情况描述:
    • 没问题。如果您对答案感到满意,请尽可能接受它。谢谢。
    【解决方案3】:

    不要绑定整个列表,试试这个(其实我不知道你的逻辑如何,但可能会对某人有所帮助)。

    taskHeader 值是一个单独的 TaskHeader 对象。

    taskDataGrid.Items.Add(taskHeader)

    【讨论】:

      猜你喜欢
      • 2012-02-24
      • 1970-01-01
      • 2016-07-06
      • 2012-07-04
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多