Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way To Span Multiple Columns in WPF Grid/List?

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

I am not sure the best way to go about this.

I currently have a ListBox displaying these user controls, but the ListBox has no header and I can't figure out how to put multiple headers on the ListBox.

Ideally I'd like something like this:

Header1   Header2  Header3   Header4
[UserControlThatSpansAllFourColumns]

My other thought was to use a DataGrid and somehow get each item to span multiple columns, but so far I can't figure that out either.

If anyone has any tips, I'd welcome them!

like image 685
Chris Holmes Avatar asked Sep 24 '10 19:09

Chris Holmes


1 Answers

Ok, this is not in any case the "best way" but I'd just throw this in. One way that sort of works like what you need is to use a ListView with a custom ItemContainerStyle that uses a <ContentPresenter> instead of the default <GridViewRowPresenter>. This short XAML somewhat demonstrates this:

<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 headers, and the items span across the entire listview. In this solution, however, the rendering of items are kind of in a world of its own. It isn't really connected to the columns defined for the ListView. So I guess one way of making this work better is to provide your own <RowPresenter> implementation that actually takes into consideration the GridViewColumns defined in the parent listview.

Anyway, hope this helps somehow.

like image 121
ASanch Avatar answered Nov 05 '22 00:11

ASanch