There is a task: in the same interface area, as a reaction to some user actions, to display tabular data of many different types. Is it reasonable to simply create two DataGrid elements each tied to their collection and hide one depending on the display of the other? Or is there some way to specify two sets of columns depending on the type of elements attached? Or maybe there is any control more suitable for my task?

  • I think it is reasonable to have a model for displaying one, and already collect different types of data into it - Dmitry
  • If AutoGenerateColumns is set to true, the DataGrid is tied to a collection of elements of a certain type, then no other type of elements can be added to this collection, and if the collection is of a more abstract type, let's assume the ancestor of all those that need to be displayed, then the columns will be generated only for the properties of this abstract type. And if you explicitly set all the necessary columns, then when displaying the second type, the columns defined for the first one will be displayed empty. Thanks for the comments. - Konstantin Galiakhmetov
  • Do you need a datagrid? Or is ItemsControl enough? Describe your task from a higher level point of view. - VladD
  • I just need to display a collection of items in tabular form with column headings. The properties of the objects are simply string and int. The ability to edit is not required. - Konstantin Galiakhmetov
  • Set 2 sets of columns - this code needs to be wise. So the easiest is ContentControl with 2 templates and each with its own DataGrid - vitidev

1 answer 1

try using this: autoGenerateColumns = true ,

  dataGrid.ItemSource = null; dataGrid.ItemSource = list1; dataGrid.ItemSource = null; dataGrid.ItemSource = list2; 
  • That is, do you propose to establish links dynamically in code? Until now, I was able to avoid accessing the view from the view model and for some reason I wanted to find an opportunity not to go further. Maybe this is my aspiration not quite justifiably and wiser to really change the connection, than for example to use two DataGrid? - Konstantin Galiakhmetov
  • I sometimes update dynamically. If you find a solution, please post it here. Still you could post an example of your code - bmo
  • if you update dynamically, dataGrid.Refresh () may be useful; - bmo