Best way to place multiple columns in a WPF / List grid?

I have a user control that I wrote in WPF to display some data. I want to show this usercontrol in the list, but I also want to provide several column headers (corresponding to some properties in the user cotrol) so that users can sort by the properties contained in usercontrol.

I am not sure if this is the best way.

I currently have a ListBox displaying these user controls, but the ListBox does not have a title, and I cannot figure out how to put multiple titles in the ListBox.

Ideally, I would like something like this:

Header1 Header2 Header3 Header4 [UserControlThatSpansAllFourColumns] 

My other thought was to use a DataGrid and somehow get each element to fit multiple columns, but so far I also can't figure it out.

If anyone has any clues, I would welcome them!

+6
wpf datagrid grid listbox
source share
1 answer

Well, this is by no means the "best way", but I would just give it up. One way this type of work, like what you need, is to use a ListView with a custom ItemContainerStyle element that uses <ContentPresenter> instead of the standard <GridViewRowPresenter> . This short XAML demonstrates this somewhat:

 <ListView> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListViewItem"> <ContentPresenter/> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListView.ItemContainerStyle> <ListView.View> <GridView> <GridViewColumn Header="Header1"/> <GridViewColumn Header="Header2"/> </GridView> </ListView.View> <Button>Item1</Button> <Button>Item2</Button> </ListView> 

Here you get the column headings, and the elements cover the entire list. In this decision, however, the rendering of items is a kind of one's own world. It really is not associated with the columns defined for the ListView. Therefore, I assume that one way to improve this work is to provide your own implementation of <RowPresenter> , which actually takes into account the GridViewColumns defined in the parent view.

In any case, I hope this helps somehow.

+3
source share

All Articles