I have a collection that I want to associate with a WPF grid.
The problem I am facing is that the number of columns is dynamic and depends on the collection. Here is a simple layout:
public interface IRows { string Message{get;} IColumns[] Columns{get;} } public interface IColumns { string Header {get;} AcknowledgementState AcknowledgementState{get;} } public interface IViewModel { ObservableCollection<IRows> Rows {get;} }
I want my view to bind to the Rows collection, which contains a collection of columns.
My Columns collection contains an enumeration that should be represented by an image (1 out of 3 possible). It also contains the Message property, which should be displayed in only one column (static and plain text information). It also contains a title bar, which should be displayed as a title for this column.

Note that the number of columns is variable (the moment the headers are set to Acknowledge, but this will change to represent dynamic data).
Update: this is after introducing offers from Rachel
<ItemsControl ItemsSource="{Binding Items, Converter={StaticResource PresentationConverter}}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Grid ShowGridLines="true" local:GridHelpers.RowCount="{Binding RowCount}" local:GridHelpers.ColumnCount="{Binding ColumnCount}" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style> <Setter Property="Grid.Row" Value="{Binding RowIndex}"/> <Setter Property="Grid.Column" Value="{Binding ColumnIndex}"/> </Style> </ItemsControl.ItemContainerStyle> <ItemsControl.ItemTemplate> <DataTemplate> <ContentControl Content="{Binding}"> <ContentControl.Resources> <DataTemplate DataType="{x:Type UI:MessageEntity}"> <TextBox Text="{Binding Message}"></TextBox> </DataTemplate> <DataTemplate DataType="{x:Type UI:StateEntity}"> <TextBox Text="{Binding State}"></TextBox> </DataTemplate> </ContentControl.Resources> </ContentControl> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
It almost gives me what I want now. I was just stuck in what I should do for the headlines. Any suggestions are welcome.
source share