我有一个绑定到 JobItems 的 ObservableCollection 的 DataGrid。JobItems 具有 DueDate 属性,并且根据 currentDate 与 DueDate 的接近程度,行颜色会相应更改。当用户通过 UI 将项目添加到 ObservableCollection 时,这非常有效,但是当我在后面的代码中创建多个虚拟 JobItem 以在程序开始时填充此数据网格时,它不起作用。然而,我添加的第一个假人完全没有问题。这很重要,因为当我的数据库就位时,我最终会需要它。
这就是我的代码在后面的样子:
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);
}
}
}
}
当我从 LoadDummies() 添加多个虚拟项目时,我收到错误:“NullReferenceException 未处理。对象引用未设置为对象实例。”
感谢所有帮助!