【问题标题】:Fill matrix per cell definition每个单元格定义填充矩阵
【发布时间】: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。

标签: c# wpf matrix grid


【解决方案1】:

您可以在 ItemsControl 中使用 Grid 作为 ItemsPanel,并在 ItemContainerStyle 中的 Style 中绑定 Grid.ColumnGrid.Row 属性。当然,列和行索引是从零开始的。

<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>

【讨论】:

  • 谢谢!但是已经用对象定义了 ColumnKey 和 RowKey。这将用于添加以任何对象为键的新 ViewModel。这不适合 Grid.Column 或 Grid.Row。而且我仍然不知道如何正确显示行和列的标题,除了添加虚拟单元格...
  • 您可以使用绑定转换器从 RowKey 和 ColumnKey 中的对象获取整数行和列索引。标头可以在外部添加,例如通过将 ItemsControl 放在外部 Grid 中。
  • 我尝试了另一种方式在 ItemsControls 中显示我的数据。标题总是问题。如果我将 DataTemplate 用于我的单元格,那将不会保持相同的水平。似乎不容易解决。
  • 如何将标题的宽度与它的列中的最大宽度绑定?我曾尝试这样做,但无法做到。那是 xaml 中的代码: Width="{Binding CellWidth, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" 。我将 CellWidth 定义为 double,但只有 NaN。
猜你喜欢
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
  • 2014-07-12
  • 2021-12-28
相关资源
最近更新 更多