如何以编程方式在C#wpf中在运行时为数据网格单元格分配或设置值 [英] How to Programmatically assign or set a value to a datagrid cell at runtime in C# wpf
问题描述
如何以编程方式在运行时在C#wpf
How to Programmatically assign or set a value to a datagrid cell at runtime in C# wpf
推荐答案
中为数据网格单元格分配或设置值,例如我使用了一个实现INotifyPropertyChanged的Student类如下所示。
for example i have used a Student class below which implements "INotifyPropertyChanged" as shown below.
public class Student : INotifyPropertyChanged
{
private string studentID = string.Empty;
public string StudentID
{
get { return studentID; }
set
{
studentID = value;
OnPropertyChanged("StudentID");
}
}
private string fName = string.Empty;
public string FirstName
{
get { return fName; }
set
{
fName = value;
OnPropertyChanged("FirstName");
}
}
private string lName = string.Empty;
public string LastName
{
get { return lName; }
set
{
lName = value;
OnPropertyChanged("LastName");
}
}
private int totalMark = 0;
public int TotalMark
{
get { return totalMark; }
set
{
totalMark = value;
OnPropertyChanged("TotalMark");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// On property changed method
/// </summary>
/// <param name="propertyName">Property Name</param>
void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
public class StudentCollection : ObservableCollection<student>
{
}
和StudentCollection的公共财产
and a public property for StudentCollection
public StudentCollection studentCollection = new StudentCollection();
并为该集合增值并分配给网格
and added value for that collection and assigned to the grid
studentCollection.Add(new Student { StudentID = "NK", FirstName = "Navin", LastName = "Kumar", TotalMark = 350 });
studentCollection.Add(new Student { StudentID = "RK", FirstName = "Ram", LastName = "Kumar", TotalMark = 350 });
studentCollection.Add(new Student { StudentID = "PK", FirstName = "Prem", LastName = "Kumar", TotalMark = 350 });
studentDG.ItemsSource = studentCollection;
点击某个按钮我正在更新标记值,如下所示
on some button click i am updating the mark value as below
private void OnUpdateMarkClicked(object sender, RoutedEventArgs e)
{
foreach (Student item in studentCollection)
{
item.TotalMark += 1;
}
}
让我知道它是否有效
let me know if it works
虽然你的问题缺乏很多细节,但我会试着让你对这种方式有所了解。
首先要欣赏的是我将使用称为MVVM的架构模式(它代表模型视图ViewModel)来讨论这个问题,这是一个非常着名的模式。它在这里使用是因为WPF与MVVM非常兼容,因为WPF中的绑定非常适合MVVM。所以,我们必须在这里做一些假设:
While your question lacks a lot of detail, I'll try to give you some understanding of the way that this is typically done.
The first thing to appreciate is that I'll be talking through this using an architectural pattern known as MVVM (it stands for Model View ViewModel), and it's a very well known pattern. It's used here because WPF works very well with MVVM, as the binding in WPF fits in very nicely with MVVM. So, we have to make some assumptions here:
- 你创建了一个实现(或继承自实现的类)的类
INotifyPropertyChanged的
。在此类中,您已创建将要映射到DataGrid的属性,并且当属性值更改时,这些属性会引发PropertyChanged
事件。 - 您创建了另一个类,其中包含
INotifyPropertyChanged
类的ObservableCollection
。 - 您已将第2点中的类实例作为
DataContext
分配给包含DataGrid
的视图。
- You have created a class that implements (or inherits from a class that implements)
INotifyPropertyChanged
. In this class, you have created properties that you will be mapping into the DataGrid, and these properties raise thePropertyChanged
event when the value of the property changes. - You have created another class that contains an
ObservableCollection
of theINotifyPropertyChanged
class. - You have assigned an instance of the class in point 2 as the
DataContext
to the view that contains yourDataGrid
.
是的,既然我们已经了解了我们需要实现的代码框架,那么您需要做的就是在第1点的属性中设置值,并且只要你从属性中提升PropertyChanged事件,你的UI就会因为绑定的神奇而更新。
Right, now that we have an understanding of what framework of code we need to put into place, all you need to do is set values in the properties in point 1, and as long as you are raising the PropertyChanged event from the property, your UI will update due to the "magic" of binding.
这篇关于如何以编程方式在C#wpf中在运行时为数据网格单元格分配或设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!