Dynamic filling of a data grid using a collection of values ​​with row and column coordinates

advertisements

I have an observable collection that has three fields:

Cell Value, Column Index, Row Index

(Edit: Each object of the list has these three fields)

Is there a way I can bind this observable collection to a Data Grid using the column index and row index to specify the location of each cell? The observable collection is in column order (first object is [row 1, col 1], second object is [row 1, col 2] etc.) if that helps.

It's important that it follows the MVVM pattern which is causing me the problem.


You might want to have a look at the UniformGrid. You can bind to Rows and Columns to tell it your dimensions (properties of your view model). Then you build around an ItemsControl and feed in your data via ItemsSource. The rest is about styling (how you want to look like your table). The following XAML might give you a starting Point. MyList here is simply a list of intfor testing.

    <ItemsControl ItemsSource="{Binding MyList}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="{Binding Rows}" Columns="{Binding Columns}"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>