How to display real tabular data using WPF?

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?

0
source share
2 answers

Well, I really have to stop working at night, a new day, a clear head, and I found a solution pretty quickly. He basically answered here Setting Canvas properties in the DataCemplate ItemsContplate already. I had to use ItemContainerStyle to set Grid.Row and Grid.Column for my processed items, or rather for wrapping ContentPresenter.

0
source

ListView, , DataGrid, , DataTable, DataGrid , .

, .

DataGrid:

<WpfToolkit:DataGrid
            IsReadOnly="True"
            IsTabStop="False"
            ItemsSource="{Binding Path=GridData, Mode=OneWay}">
</WpfToolkit:DataGrid>

XAML ( MVVM):

private DataTable _dt = new DataTable("MyDataTable");
public DataView GridData
{
   get
   {
      return _dt.DefaultView;
   }
}

DataColumns DataTable :

DataColumn fileType = new DataColumn("FileType");
fileType.AllowDBNull = true;
fileType.DataType = typeof(string);
_dt.Columns.Add(fileType);

DataRows DataTable :

DataRow dr = _dt.NewRow();
dr["FileType"] = "*.txt";
_dt.Rows.Add(dr);

Reset :

_dt.Rows.Clear();
_dt.Columns.Clear();

- , DataGrid.

0

All Articles