跨度在WPF网格/列表中多列的最佳方式? [英] Best Way To Span Multiple Columns in WPF Grid/List?

查看:209
本文介绍了跨度在WPF网格/列表中多列的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义用户控件,我在WPF中编写来显示一些数据。我想在列表中显示此用户控件,但我也想提供多个列标题(匹配用户cotrol上的某些属性),以便用户可以对包含在usercontrol中的属性进行排序。

我不确定最好的方式来解决这个问题。

我目前有一个ListBox显示这些用户控件,但ListBox没有标题,我不知道如何在ListBox上放置多个标题。



理想情况下,我想要这样:

  Header1 Header2 Header3 Header4 
[UserControlThatSpansAllFourColumns]

我的另一个想法是使用DataGrid并以某种方式获取每个项目跨越多列,但到目前为止我无法弄清楚。



如果有人有任何提示,我会欢迎他们! 好吧,这不是任何情况下的最好的方式,但我只是把它扔进去。一种像你需要的方式工作的方式是使用带有自定义ItemContainerStyle的ListView,它使用< ContentPresenter> ,而不是默认的< GridViewRowPresenter> 。这个简短的XAML有点证明了这一点:

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

<按钮>项目1< /按钮>
<按钮>项目2< /按钮>

< / ListView>

在这里,您可以获得列标题,并且项目跨越整个列表视图。然而,在这个解决方案中,物品的渲染在它自己的世界中是有点类似的。它没有真正连接到为ListView定义的列。所以我想一个更好的做法是提供你自己的< RowPresenter> 实现,它实际上考虑了在父列表视图中定义的GridViewColumns。



无论如何,希望这有助于某种方式。


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!

解决方案

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.

这篇关于跨度在WPF网格/列表中多列的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆