不带INotifiedChanges的wpf数据网格刷新
我在wpf中使用了datagrid,在.xaml.cs中使用了一些代码不带INotifiedChanges的wpf数据网格刷新,wpf,datagrid,refresh,Wpf,Datagrid,Refresh,我在wpf中使用了datagrid,在.xaml.cs中使用了一些代码 List<TaskHeader> taskHeaders; //initialization of taskHeaders taskDataGrid.ItemsSource = taskHeaders; 它也不起作用 有什么想法吗?请帮助尝试 CollectionViewSource.GetDefaultView(taskHeaders).Refresh(); 我已经测试过这一点,这一点很有效: 我的XAML
List<TaskHeader> taskHeaders;
//initialization of taskHeaders
taskDataGrid.ItemsSource = taskHeaders;
它也不起作用
有什么想法吗?请帮助尝试
CollectionViewSource.GetDefaultView(taskHeaders).Refresh();
我已经测试过这一点,这一点很有效: 我的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>
与其绑定整个列表,不如试试这个(实际上我不知道你的逻辑如何,但可能会对某人有所帮助) taskHeader值是一个taskHeader对象
taskDataGrid.Items.Add(taskHeader)为什么您不想使用真正有效的方法呢?谢谢Stipo,您选择我的方式来理解按钮单击事件或其他用户操作是在DataGrid上刷新所必需的,以下是所有情况说明:没问题。如果您对答案感到满意,请尽可能接受它。谢谢
<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();
}
}
}
namespace WpfApplication
{
public class TaskHeader
{
public string Property { get; set; }
}
}