WPF Datagrid刷新显示成员路径为空 [英] WPF Datagrid refresh Displaymember path empty

查看:167
本文介绍了WPF Datagrid刷新显示成员路径为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想刷新我的DataGrid并在网络上找到以下代码:

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

除了字符串/列之外它还有效名称不会与对象一起显示对象/



编辑



 < DataGridTextColumn Binding ={Binding TId}Header =idMinWidth =20MaxWidth =60 /> 
< DataGridTextColumn Binding ={Binding TChassisManufacturer}Header =Project NameMinWidth =122MaxWidth =200/>
< DataGridTextColumn Binding ={Binding ProjectStatusM}Header =StatusMinWidth =122MaxWidth =100/>


解决方案

以下是一个示例:



您不需要调用刷新。当您从我的添加/删除项目时, 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 =dataGridViewItemsSource ={Binding}AutoGenerateColumns =False>
< DataGrid.Columns>
< DataGridTextColumn Binding ={Binding TId}Header =idMinWidth =20MaxWidth =60/>
< DataGridTextColumn Binding ={Binding TChassisManufacturer}Header =Project NameMinWidth =122MaxWidth =200/>
< DataGridTextColumn Binding ={Binding ProjectStatusM}Header =StatusMinWidth =122MaxWidth =100/>
< /DataGrid.Columns>
< / DataGrid>
< / Window>

C#:

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

字符串名称;
public string TChassisManufacturer
{
get
{
返回名称;
}
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));
}
}
}


I want to Refresh my DataGrid and found the following coding on the web:

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

It does work except for the string/column Names does not get displayed with the objects only the objects/items itself.

Any Ideas on why this is happening?

EDIT:

 <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"/>

解决方案

Here is an example:

You don't need to call refresh. When you add/delete items from my, dg will auto update for you.

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

这篇关于WPF Datagrid刷新显示成员路径为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆