0

我在 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();

是不是工作太有想法了?请帮忙

4

3 回答 3

0

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

taskHeader 值是一个 TaskHeader 对象。

taskDataGrid.Items.Add(taskHeader)

于 2014-12-25T14:36:43.823 回答
0

尝试

CollectionViewSource.GetDefaultView(taskHeaders).Refresh();
于 2012-05-22T14:44:02.720 回答
0

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

我的 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; }
    }
}
于 2012-05-22T15:17:52.017 回答