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

拈花ヽ惹草 提交于 2019-12-22 05:39:09

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/3790117/best-way-to-span-multiple-columns-in-wpf-grid-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!