填充每个单元格定义的矩阵 [英] Fill matrix per cell definition

查看:127
本文介绍了填充每个单元格定义的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在像矩阵这样的视图中显示和编辑我的数据。所有的数据将被收集在 ObserverableCollection 中。然后我将填充每个单元格定义的集合。每个单元格定义都有它的行和列的关键字以及一个字符串,它应该显示在矩阵中。

I need to show and edit my data in a view like matrix. All the data will be collected in a ObserverableCollection. Then i will fill the collection per cell definition. Each cell definition has the key of it's row and column, and a string, which should be shown in the matrix.

public class CellViewModel : ViewModelBase
{
    public object RowKey { get; set; }    
    public object ColumnKey { get; set; }
    public string CellText { get; set; }
}

这意味着,当一个 CellViewModel 被添加到集合中,并且具有相同RowKey的行已经存在,则该单元格将被填充到该行的合适列中。如果该列不存在,则会添加一个新列。关键可以是任何对象,该人可以添加任何对象键的单元格,而不需要关心设置单元格的正确位置。例如,我使用 CellViewModels 填充集合:

That means, when a CellViewModel is added to the collection, and a row with same RowKey is already existed, the cell will be filled to suitable column of this row. If the column is not existed, a new column will be added. The key could be any object, with that man can add a cell with any object keys and don't need to care about setting right position of cell. For example, i fill the collection with 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"
    }
}

然后矩阵应该看起来像这样:

Then the matrix should look like that:

        column1 column2 column3
row1    Cell1_1         Cell1_3
row2            Cell2_2
row3    Cell3_1 Cell3_2

h ItemsControl并将整个集合转换为适合ItemsControl与place-holder ...

I have tried with ItemsControl and convert the whole collection to suit the ItemsControl with place-holder...

var list = new List<CellViewModel>{ null, null, cellViewModel };

但我认为这不是一个好主意。而且地方持有人不会保留这个地方,以保持整个矩阵良好的秩序。除了ItemsControl没有行和列的标题。

But I think that is not a good idea. And the place-holder will not reserve the place to hold the whole matrix in good order. Besides ItemsControl has no header for rows and columns.

任何想法或建议?我应该使用哪种控件?

Any idea or suggestion? With which control should I do that?

推荐答案

您可以在ItemsControl中使用Grid作为ItemsPanel,并绑定 Grid.Column Grid.Row 属性< ItemContainerStyle 中的样式。

You may use a Grid as ItemsPanel in your ItemsControl, and bind the Grid.Column and Grid.Row properties in a Style in ItemContainerStyle. Of course column and row indices are zero-based.

<ItemsControl ItemsSource="{Binding GridCellViewModelCollection}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid IsItemsHost="True">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Grid.Column" Value="{Binding ColumnKey}"/>
            <Setter Property="Grid.Row" Value="{Binding RowKey}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding CellText}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这篇关于填充每个单元格定义的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆