【问题标题】:WPF Datagrid refresh Displaymember path emptyWPF Datagrid 刷新 Displaymember 路径为空
【发布时间】:2016-06-05 05:48:25
【问题描述】:

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

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

【问题讨论】:

  • 你还必须绑定到 XAML 中的源代码
  • 是的,当我填充数据网格时,列是绑定的,它只在刷新数据网格时才起作用,除非我必须进行另一个绑定,否则它什么也不显示? @樱花
  • 你能发布你的 XAML 代码来展示你是如何绑定的吗?
  • 更新帖子@Sakura
  • 我的回答对你有帮助吗?

标签: c# wpf c#-4.0 datagrid wpf-controls


【解决方案1】:

这是一个例子:

您不需要调用刷新。当您从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"));
        }
    }
}

【讨论】:

  • 我会尝试一下,如果效果更好,那么我会给你点赞。 :)
  • @RGdent 好的。祝你好运。
猜你喜欢
  • 2012-12-09
  • 2013-11-06
  • 2010-12-19
  • 2010-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 2014-07-27
相关资源
最近更新 更多