【问题标题】:List of DataGrid Not updatingDataGrid 列表未更新
【发布时间】:2020-08-05 07:58:44
【问题描述】:

我有一个 DataGrid 列表。我正在从我的 DataTables 列表(选定索引)转移到 DataGrids 列表。在代码中,我可以监控 itemsource 被传输的 Datatable 但是,它在窗口屏幕上不可见。 这是我的代码:

m_AllDgTag[item.ID].ItemsSource = MainWindow.dataTables[item.ID].DefaultView;

其中:m_AllDgTag 是我的数据网格列表

  • 我已经尝试过刷新命令,但是没有成功。

【问题讨论】:

  • 先给ItemsSource赋值为null
  • @SilnyToJa 已经尝试过了,但是没有用。
  • 您是否以某种方式声明列?或者您只是有一个数据网格,您将其引用保存在一个列表中并为其分配一个源。
  • @SilnyToJa 我只有一个 DataGrids 列表,我一直将其引用分配为 DataTables 列表中的源。
  • 什么是m_AllDgTag[item.ID],它与您在屏幕上看到的ItemsControl 有什么联系?

标签: c# wpf datagrid itemsource


【解决方案1】:

以这种方式分配源没有问题。您的代码中可能缺少某些内容。你可以比较一下。

<DataGrid x:Name="dataGrid1" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
    </DataGrid.Columns>
</DataGrid>

public partial class MainWindow : Window
{
    public class Person
    {
        public string Name { get; set; }
    }
    List<DataGrid> dataGridsList = new List<DataGrid>();
    List<Person> per = new List<Person>();
    List<Person> per2 = new List<Person>();
    public MainWindow()
    {
        InitializeComponent();
        dataGridsList.Add(dataGrid1);
        per.Add(new Person() { Name = "a" });
        per.Add(new Person() { Name = "b" });
        dataGridsList[0].ItemsSource = per;
        per2.Add(new Person() { Name = "c" });
        dataGridsList[0].ItemsSource = per2;
        

    }
}

【讨论】:

    猜你喜欢
    • 2012-12-04
    • 2015-12-04
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多