我正在尝试做简单的任务应用程序。我有截止日期的任务,我想这样显示:
我知道如何在列表框中显示数据,但我不知道如何按 DueDate 对它们进行分组并显示 DueDates 之类的标题(如下图所示)。
这是我的带有任务的 XML 文件 (tasks.xml):
<?xml version="1.0" encoding="utf-8"?>
<Tasks>
<Task>
<Name>first task</Name>
<DueDate>05/03/2013 00:00:00</DueDate>
<Created>03/27/2013 01:24:08</Created>
</Task>
<Task>
<Name>second task</Name>
<DueDate>05/17/2013 00:00:00</DueDate>
<Created>03/27/2013 01:24:19</Created>
</Task>
<Task>
<Name>third task</Name>
<DueDate>05/17/2013 00:00:00</DueDate>
<Created>03/27/2013 01:24:38</Created>
</Task>
</Tasks>
这是我的数据模型(MainViewModel.cs):
public class MainViewModel : INotifyPropertyChanged
{
public ObservableCollection<Task> Tasks { get; private set; }
public MainViewModel()
{
this.Tasks = new ObservableCollection<Task>();
}
public bool IsDataLoaded
{
get;
private set;
}
public void LoadData()
{
try
{
Tasks.Clear();
var file = IsolatedStorageFile.GetUserStoreForApplication();
XElement xElem;
using (IsolatedStorageFileStream read = file.OpenFile("tasks.xml", FileMode.Open))
{
xElem = XElement.Load(read);
}
var tasks = from task in xElem.Elements("Task")
select task;
foreach (XElement xElemItem in tasks)
{
string dueDate = "";
DateTime convertedDate;
if (DateTime.TryParse(xElemItem.Element("DueDate").Value.ToString(), out convertedDate))
{
dueDate = convertedDate.ToShortDateString();
}
Tasks.Add(new Task
{
Name = xElemItem.Element("Name").Value.ToString(),
DueDate = dueDate,
Created = xElemItem.Element("Created").Value.ToString()
});
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.IsDataLoaded = true;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我该怎么做?