I have the following problem: I have to display tabular data in a grid.
Say class A is a data element with properties Foo and Bar, and I have a list of List or sth elements like this. I want to display these elements in a grid, where elements with the same Foo are in the same column, and elements with the same column in the same row. main. simply. I thought?
My initial attempt was to create an ItemsControl, use the Grid as ItemsPanelTemplate and ItemTemplate, where I linked the Grid.Row and Grid.Column properties to A.Foo and A.Bar, using IValueConverters to bind a specific Foo to a specific column, e.g. the converter will look for the Foo value as an array of Columns and return the correct index. The thing is, building rows and columns for the Grid. Since they are completely dynamic (I don’t know at design time which Foo and Bar will be in the source collection), I cannot define them in the xaml file. But at run time, the ItemsControl really does not provide access to the current instance of the ItemsPanel. The only way I found was dirty using VisualTreeHelper.GetChild,and I managed to get an instance of ItemsPanel and create the necessary Row and ColumnDefinitions parameters, but ItemTemplate-d elements just won’t appear in the right row and column, although my converters returned the correct column / row indices. Maybe I really do not understand how ItemsControl works internally, but the fact is that this is not an idea.
Someone suggested using ListView with GridView, grouping my elements of one bar in another object, this will make it easier for me to dynamically create rows, but dynamically creating columns seems rather complicated.
Does anyone have an idea or at least a hint? Am I missing something obvious?
source
share