WPF 数据网格刷新显示成员路径为空

本文关键字:路径 成员 刷新显示 数据 数据网 网格 WPF | 更新日期: 2023-09-27 18:36:00

我想刷新我的 DataGrid,并在 Web 上找到以下编码:

dataGridView.ItemsSource = null;
dataGridView.ItemsSource = ItemsSourceObjects;

它确实有效,除了字符串/列名称不与对象一起显示,只有对象/项目本身。

关于为什么会发生这种情况的任何想法?

编辑:

 <DataGridTextColumn Binding="{Binding TId}" Header="id" MinWidth="20" MaxWidth="60"/>
 <DataGridTextColumn Binding="{Binding TChassisManufacturer}" Header="Project Name" MinWidth="122" MaxWidth="200"/>
 <DataGridTextColumn Binding="{Binding ProjectStatusM}" Header="Status" MinWidth="122" MaxWidth="100"/>

WPF 数据网格刷新显示成员路径为空

下面是

一个示例:

无需调用刷新。当您在my中添加/删除项目时,dg将自动更新。

XAML:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1">
    <DataGrid Name="dataGridView" ItemsSource="{Binding}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding TId}" Header="id" MinWidth="20" MaxWidth="60"/>
            <DataGridTextColumn Binding="{Binding TChassisManufacturer}" Header="Project Name" MinWidth="122" MaxWidth="200"/>
            <DataGridTextColumn Binding="{Binding ProjectStatusM}" Header="Status" MinWidth="122" MaxWidth="100"/>
        </DataGrid.Columns>
    </DataGrid>
</Window>

C#:

public partial class MainWindow : Window
{
    ObservableCollection<myClass> my = new ObservableCollection<myClass>();
    public MainWindow()
    {
        InitializeComponent();
        dataGridView.DataContext = my;
        my.Add(new myClass { TId = 1, TChassisManufacturer = "Sony", ProjectStatusM = "Done" });
        my.Add(new myClass { TId = 2, TChassisManufacturer = "Apple", ProjectStatusM = "Doing" });
    }
}
class myClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    int id;
    public int TId
    {
        get
        {
            return id;
        }
        set
        {
            id = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("TId"));
        }
    }
    string name;
    public string TChassisManufacturer
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("TChassisManufacturer"));
        }
    }
    string status;
    public string ProjectStatusM
    {
        get
        {
            return status;
        }
        set
        {
            status = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ProjectStatusM"));
        }
    }
}