简体   繁体   English

在WPF网格/列表中跨越多列的最佳方法?

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

I have a custom user control I wrote in WPF to display some data. 我有一个我在WPF中编写的自定义用户控件来显示一些数据。 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. 我想在列表中显示此usercontrol,但我还想提供多个列标题(匹配用户cotrol上的一些属性),以便用户可以对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. 我目前有一个ListBox显示这些用户控件,但ListBox没有标题,我无法弄清楚如何在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. 我的另一个想法是使用DataGrid并以某种方式让每个项目跨越多个列,但到目前为止,我也无法解决这个问题。

If anyone has any tips, I'd welcome them! 如果有人有任何提示,我会欢迎他们!

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> . 好吧,这在任何情况下都不是“最好的方法”,但我只是把它扔进去。有一种方法可以像你需要的那样使用带有自定义ItemContainerStyle的ListView,它使用<ContentPresenter>而不是default <GridViewRowPresenter> This short XAML somewhat demonstrates this: 这个简短的XAML有点证明了这一点:

<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. 它并没有真正连接到为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. 所以我想一种使这项工作更好的方法是提供你自己的<RowPresenter>实现,它实际上考虑了父列表视图中定义的GridViewColumns。

Anyway, hope this helps somehow. 无论如何,希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM