【发布时间】:2014-03-05 14:25:07
【问题描述】:
我需要在矩阵等视图中显示和编辑我的数据。所有数据都将收集在ObserverableCollection 中。然后我将填充每个单元格定义的集合。每个单元格定义都有它的行和列的键,以及一个字符串,应该显示在矩阵中。
public class CellViewModel : ViewModelBase
{
public object RowKey { get; set; }
public object ColumnKey { get; set; }
public string CellText { get; set; }
}
也就是说,当CellViewModel被添加到集合中,并且已经存在具有相同RowKey的行时,单元格将被填充到该行的合适列。如果该列不存在,将添加一个新列。键可以是任何对象,该人可以使用任何对象键添加单元格,而无需关心设置单元格的正确位置。例如,我用CellViewModels 填充集合:
GridCellViewModelCollection = new ObservableCollection<CellViewModel>
{
new CellViewModel
{
RowKey = 1,
ColumnKey = 1,
CellText = "Cell1_1"
},
new CellViewModel
{
RowKey = "string",
ColumnKey = 'char',
CellText = "Cell2_2"
},
new CellViewModel
{
RowKey = 3,
ColumnKey = 1,
CellText = "Cell3_1"
},
new CellViewModel
{
RowKey = 1,
ColumnKey = 3,
CellText = "Cell1_3"
},
new CellViewModel
{
RowKey = 3,
ColumnKey = 'char',
CellText = "Cell3_2"
}
}
那么矩阵应该是这样的:
column1 column2 column3
row1 Cell1_1 Cell1_3
row2 Cell2_2
row3 Cell3_1 Cell3_2
我已尝试使用 ItemsControl 并将整个集合转换为适合带有占位符的 ItemsControl...
var list = new List<CellViewModel>{ null, null, cellViewModel };
但我认为这不是一个好主意。并且占位符不会保留保持整个矩阵有序的位置。此外,ItemsControl 没有行和列的标题。
有什么想法或建议吗?我应该使用哪个控件来执行此操作?
【问题讨论】:
-
您是否考虑过在 ItemsControl 中使用 Grid 作为 ItemsPanel?
-
@Clemens:我不知道。我试过没有 ItemsControl 和 GridView 的 Grid。