我正在接受一些WPF建议;我有一个定义时间,事件类型和描述的对象列表,如下所示:
public class AnEvent
{
public EventType EventType { get; set; }
public DateTime TimeStamp { get; set; }
public string Description { get; set; }
}
我想显示这样的项目集合:
-------------------------------------
| Time | Type1 | Type3 |
-------------------------------------
| 12:00 | Event1 | |
-------------------------------------
| 12:01 | Event2 | Event4 |
| | Event3 | |
-------------------------------------
| 12:05 | | Event5 |
-------------------------------------
在WPF中显示此数据的最佳方法是什么?
我考虑使用网格,但我不确定如何
我还考虑过预处理事件列表以按时间生成事件集合,然后按类型生成事件集合,然后使用多个ItemsControls显示不同级别。
我的其他选择是什么?
谢谢!
答案 0 :(得分:0)
我建议您按原样编写一个简单的虚拟机
public class EventLogByTimeViewModel : ViewModelBase
{
public DateTime Time { get; set; }
public ObservableCollection<AnEvent> Type1Collection { get; set; }
public ObservableCollection<AnEvent> Type3Collection { get; set; }
}
创建一个工厂,您将提供一个AnEvent列表,并输出一个VM列表
public interface IEventLogByTimeVMFactory
{
IList<EventLogByTimeViewModel> GenerateVM(IList<AnEvent>);
}
您可以在工厂实施中使用Linq扩展中的GroupBy
方法,以便更轻松地完成此过程。
然后在您的高级视图模型(绑定到DataContext的模型)中,按原样公开并实现EventLogByTimeViewModel的集合
public class MainViewModel : ViewModelBase
{
private IEventLogByTimeVMFactory _factory;
public MainViewModel()
{
EventLogs = new ObservableCollection<EventLogByTimeViewModel>();
_factory = new MyFactoryImplementation();
}
public ObservableCollection<EventLogByTimeViewModel> EventLogs { get; set; }
public void LoadData(IList<EventEventLogByTimeViewModel> eventLogs)
{
var vms = _factory.GenerateVM(eventLogs);
EventLogs.Clear();
foreache(vm in vms)
{
EventLogs.Add(vm);
}
}
}
您将调用LoadData方法的机制取决于您。
最后在您的用户界面中,我建议您使用DataGrid
控件,将EventLogs
属性绑定到其ItemsSource
,最后为Type1和Type3列实现DataTemplate
这将在EventType
内显示ItemsControl
个集合。