如何在WPF中使用行索引和列索引将值分配给DataGrid单元格 [英] How to Assign values to DataGrid Cells Using their Row Index And Column Index in WPF

查看:113
本文介绍了如何在WPF中使用行索引和列索引将值分配给DataGrid单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 大家好

 实际上我是WPF的新手.刚刚开始从事这项技术的工作.

  Actually i Am very new in WPF. Just Started Work on this Technology .

 我现在想了解WPF的DataGrid控件.如何在DataGrid控件中使用行索引和列索引获取或设置值,以及如何在ENTER键上添加新行.

  i want to now about DataGrid Control of WPF. How to Get Or Set Values Using Row Index And Column Index in DataGrid Control and How to Add new row on ENTER Key Press.

   请帮助解决此问题

   Please Help about This Problem

   谢谢

   Thanks In Advance

  Pawan Shende

  Pawan Shende 

推荐答案

您应该将DataGrid的ItemsSource属性设置或绑定到某些对象集合,然后编辑这些对象的属性,而不是修改实际的DataGrid.

You should set or bind the ItemsSource property of the DataGrid to some collection of objects and then edit the values of the properties of these objects rather than modifying the actual DataGrid.

例如,如果要在DataGrid中显示Employee对象的列表,则可以将ItemsSource属性设置为Employee对象的集合,然后修改某个Employee对象的属性:

If you for example want to display a list of Employee objects in the DataGrid, you could set the ItemsSource property to a collection of Employee objects and then modify the properties of a certain Employee object:

    class Employee : INotifyPropertyChanged
    {
        public string name;
        public string Name
        {
            get { return name; }
            set { name = value; NotifyPropertyChanged("Name"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }


        <DataGrid x:Name="dataGrid"/>


            ObservableCollection<Employee> emps = new ObservableCollection<Employee>();
            emps.Add(new Employee() { Name = "Name...." });
            dataGrid.ItemsSource = emps;

            emps[0].Name = "new name...";

请注意,Employee类需要实现INotifyPropertyChanged接口,以便任何动态更改自动显示在视图中: http://msdn.microsoft.com/zh-CN/library/vstudio/system.componentmodel.inotifypropertychanged

Note that the Employee class needs to implement the INotifyPropertyChanged interface for any dynamic changes to be automatically displayed in the view: http://msdn.microsoft.com/en-us/library/vstudio/system.componentmodel.inotifypropertychanged

还请注意,如果您希望能够在运行时向集合中添加项目或从集合中删除项目,则应使用ObservableCollection: http://msdn.microsoft.com/en-us/library/ms668604 (v = vs.110).aspx

Also note that you should use an ObservableCollection if you want to be able to add or remove items to/from the collection during runtime: http://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx

如果DataGrid的CanUserAddRows属性设置为默认值true,并且Employee类(在这种情况下)具有默认值,则用户可以通过在最后一个空白行上输入值来向源集合中添加新的Employee对象.默认 无参数构造函数.

The user can add a new Employee object to the source collection by entering values on the last blank row provided that the CanUserAddRows property of the DataGrid is set to its default value of true and that the Employee class (in this case) has a default parameterless constructor.


这篇关于如何在WPF中使用行索引和列索引将值分配给DataGrid单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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