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

发布于 2024-09-24 23:59:17 字数 389 浏览 0 评论 0原文

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

我不确定解决这个问题的最佳方法。

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

理想情况下,我想要这样的东西:

Header1   Header2  Header3   Header4
[UserControlThatSpansAllFourColumns]

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

如果有人有任何提示,我会欢迎他们!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦初醒的夏天 2024-10-01 23:59:17

好吧,这在任何情况下都不是“最好的方法”,但我只是把它扔进去。一种像您需要的那样工作的方法是将 ListView 与使用 的自定义 ItemContainerStyle 一起使用; 而不是默认的 。这个简短的 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>

在这里,您可以获得列标题,并且项目跨越整个列表视图。然而,在这个解决方案中,项目的渲染有点像在它自己的世界中。它并没有真正连接到为 ListView 定义的列。因此,我想使这项工作更好的一种方法是提供您自己的 实现,该实现实际上考虑了父列表视图中定义的 GridViewColumns。

不管怎样,希望这能有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文