[英]Cannot add more than one row to datagrid with different colors WPF c#
I have a DataGrid that is bound to an ObservableCollection of JobItems. 我有一个绑定到JobItems的ObservableCollection的DataGrid。 JobItems have a DueDate property, and depending on how close the currentDate gets to the DueDate, the row colors change accordingly.
JobItems具有DueDate属性,并且根据currentDate与DueDate的接近程度,行颜色也会相应更改。 This works perfectly when the user adds items to the ObservableCollection through a UI, but it does not work when I create more than one dummy JobItem in the code behind to populate this datagrid at the start of the program.
当用户通过UI将项目添加到ObservableCollection时,这种方法非常有效,但是当我在后面的代码中创建多个虚拟JobItem并在程序开始时填充此datagrid时,此方法将无法正常工作。 The first dummy I add, however, has no problems at all.
但是,我添加的第一个虚拟对象完全没有问题。 This is important because I will eventually need this when I have my database in place.
这很重要,因为在拥有数据库之后,最终将需要使用它。
This is what my code looks like behind: 这是我的代码的样子:
ObservableCollection <JobItem> JobItemList = new ObservableCollection<JobItem>();
public AdminView(Employee currEmployee)
{
this.Title = "Administrator View - " + currEmployee.Name;
InitializeComponent();
jobItemDataGrid.DataContext = JobItemList;
LoadDummies();
}
private void LoadDummies()
{
JobItem j1 = new JobItem();
JobItem j2 = new JobItem();
JobItem j3 = new JobItem();
j1.WorkOrderNo = "W12-021";
j2.WorkOrderNo = "W12-037";
j3.WorkOrderNo = "W12-234";
j1.DateReceived = DateTime.Now.ToString();
j2.DateReceived = DateTime.Now.ToString();
j3.DateReceived = DateTime.Now.ToString();
j1.DueDate = (DateTime.Now.AddDays(2)).ToString();
j2.DueDate = (DateTime.Now.AddDays(2)).ToString();
j3.DueDate = (DateTime.Now.AddDays(9)).ToString();
j1.RushPriority = true;
j1.Status = "Completed";
j2.Status = "Accepted";
j3.Status = "Completed";
JobItemList.Add(j1);
JobItemList.Add(j2);
JobItemList.Add(j3);
}
private void newJobBtn_Click(object sender, RoutedEventArgs e)
{
NewJobDialog newJobWindow = new NewJobDialog(workOrderCounter);
newJobWindow.ShowDialog();
if (newJobWindow.DialogResult == true)
{
if (newJobWindow.HasContent && newJobWindow.DialogResult.Value)
{
JobItemList.Add(newJobWindow.newJob);
workOrderCounter++;
}
}
}
/*Formats each row in the data grid depending on how close the due date of each
* job is. Each color corresponds to how close the due date is from today.
* Blue: Job is due in more than 7 days
* Green: Job is due between 4-7 days
* Yellow: Job is due in 0-3 days
* Red:
* */
private void jobItemDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
DateTime currentDate = DateTime.Now;
DateTime dateValue = new DateTime();
TimeSpan elapsed;
Double daysLeft;
Double enoughTime = 7;
Double dueSoon = 3;
Double dueToday = 0;
foreach (JobItem item in jobItemDataGrid.ItemsSource)
{
var row = jobItemDataGrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
//subtracting the two dates to get an elapsed amount of days
if ((DateTime.TryParse(item.DueDate.ToString(),out dateValue) == true))
{
//dateValue = DateTime.Parse(item.DueDate.ToString());
elapsed = dateValue.Subtract(currentDate);
daysLeft = elapsed.TotalDays;
//MessageBox.Show("due date: " + dateValue.ToString() + " current date: " + currentDate.ToString() + " days left: " + daysLeft.ToString() + "rounded: " + daysLeftRound.ToString());
if (item.RushPriority == true)
{
row.Background = new SolidColorBrush(Colors.Black);
row.Foreground = new SolidColorBrush(Colors.White);
}
else if (daysLeft > enoughTime)
{
row.Background = new SolidColorBrush(Colors.Blue);
row.Foreground = new SolidColorBrush(Colors.White);
}
else if (daysLeft <= enoughTime && daysLeft > dueSoon)
{
row.Background = new SolidColorBrush(Colors.Green);
row.Foreground = new SolidColorBrush(Colors.White);
}
else if (daysLeft <= dueSoon && daysLeft >= dueToday)
{
row.Background = new SolidColorBrush(Colors.Yellow);
}
else if (daysLeft < dueToday)
{
row.Background = new SolidColorBrush(Colors.Red);
row.Foreground = new SolidColorBrush(Colors.White);
}
}
}
}
I when I add more than one dummy item from LoadDummies(), I get the error: "NullReferenceException was unhandled. Object reference not set to an instance of object." 当我从LoadDummies()添加多个虚拟项目时,出现错误:“未处理NullReferenceException。对象引用未设置为对象的实例。”
All help is appreciated! 感谢所有帮助!
I think you forgot to create your JobItemList. 我认为您忘记了创建JobItemList。 try putting this in your constructor:
尝试将其放在您的构造函数中:
JobItemList = new List<JobItem>();
在下面的if语句中,添加一个空检入行
if ((DateTime.TryParse(item.DueDate.ToString(), out dateValue) == true) && row != null)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.