Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不带INotifiedChanges的wpf数据网格刷新_Wpf_Datagrid_Refresh - Fatal编程技术网

不带INotifiedChanges的wpf数据网格刷新

不带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

我在wpf中使用了datagrid,在.xaml.cs中使用了一些代码

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