单击按钮更新wpf datagrid [英] updating wpf datagrid on button click

查看:95
本文介绍了单击按钮更新wpf datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#相当陌生,并且正在尝试一些非常基本的事情。我发现使用datagrid的示例更为复杂,我只想要一个列表,只要按一下按钮就可以勾出该列表并添加时间戳。关于如何最好地做到这一点的任何想法?目前,我有以下内容显然不起作用。

I am reasonably new to C# and am struggling with some very basic things. The examples of using datagrid that I have found have been more complex, I just want a list which I can 'tick off' at the press of a button and add a timestamp to. Any ideas on how best to do this? Currently I have the following which obviously doesn't work.

C#

    public class ListData
    {
        public int Number { get; set; }
        public string Signature { get; set; }
    }

    List<ListData> LoadListData()
    {
        List<ListData> TableInfo = new List<ListData>();
        TableInfo.Add(new ListData()
        {
            Number = 1,
        });
        TableInfo.Add(new ListData()
        {
            Number = 2,
        });
        TableInfo.Add(new ListData()
        {
            Number = 3,
        });
        return TableInfo;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DataGrid1.ItemsSource = LoadListData();
    }

    private void NextRow_Click(object sender, RoutedEventArgs e)
    {
        //update row i here
        //add Signature =  "J Doe " + DateTime.Now,
        // i++
    }

XAML

    <DataGrid Height="200" HorizontalAlignment="Right" Name="DataGrid1" VerticalAlignment="Top" Width="400" />


推荐答案

一个简单的解决方案是添加一个Timestamp属性(类型 DateTime 的类型添加到 ListData 类中。单击按钮后,您必须遍历现有集合并添加时间戳记值,如下所示:

A simple solution would be to add a Timestamp property (of type DateTime) to your ListData class. Once the button is clicked, you have to loop through your existing collection and add the timestamp value, like this:

var myList = LoadListData();

foreach(var item in myList)
{
   item.Timestamp = DateTime.Now;
}

在循环结束时,您必须执行以下操作:

At the end of the loop, you have to do something like this:

DataGrid1.ItemsSource = myList;
DataGrid1.Items.Refresh(); //to refresh the rows in the DataGrid

更好的解决方案是使用 ObservableCollection 并实施 INotifyPropertyChanged 在您的ListData类中,使用此伪代码:

A better solution is to use an ObservableCollection of your items and to implement the interface INotifyPropertyChanged in your ListData class, using this pseudo-code:

    public class ListData : INotifyPropertyChanged
    {

    public event PropertyChangedEventHandler PropertyChanged;
    private int _number;

    public int Number 
    { 
    get
    {
      return _number;
    } 
    set
    {
       if(value!= null)
    {
      _number = value;
      OnPropertyChanged("Number");
    }
    }

private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        } 
}


    public ObservableCollection<ListData> MyList {get;set;}

通过这种方式,您只需修改以下项的Timestamp属性

In this way, you will have only to modify the Timestamp property of each item of your collection, without updating manually the DataGrid.

有关ObservableCollection的更多信息此处

Further info about the ObservableCollection here.

这篇关于单击按钮更新wpf datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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