我已经对此进行了测试,并且有效:
我的 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; }
}
}