具有多行标题的 WPF DataGrid

发布于 2024-11-09 10:42:39 字数 3052 浏览 0 评论 0原文

我有一个非常奇怪的网格的设计要求......看起来像这样:

grid http://img219.imageshack.us/img219/3444/columns.png

我需要能够对两行中的标题进行排序...因此,如果他们单击“描述”,则会按描述进行排序。如果他们单击“状态”,则会按状态排序。在列标题上使用模板,我可以实现这种外观,但我似乎无法找到一种方法来让每个单独的单元充当不同的标题。我到底该怎么做呢?

编辑:为了创建列标题,我这样做了:

<StackPanel Orientation="Vertical" >
     <Border Margin=".5" Background="{StaticResource DarkBlueBackground}" >
           <Button Margin="6" Style="{StaticResource StaticLinkButton}"">
                <TextBlock HorizontalAlignment="Left" Text="Part Number" MinWidth="40"  />
           </Button>
     </Border>

     <Border Margin=".5" Background="{StaticResource LightBlueBackground}" MinWidth="120">
            <Button  Margin="6" Style="{StaticResource StaticLinkButton}" Content="Part Type"/>
     </Border>
 </StackPanel>

,这在大多数情况下都有效。问题是,当我有一个特别长的数据项(如长“部分类型”)时,它会拉伸列,但“部分类型”标题周围的边框不会拉伸。我也尝试使用网格而不是堆栈面板,但结果相同。是否有一种相对简单的方法可以使标题中的边框/按钮/文本块延伸到列的整个宽度?

编辑2
显示每个请求的标头模板代码:

<DataGridTemplateColumn.HeaderTemplate>
    <DataTemplate>
        <StackPanel Orientation="Vertical" >
            <StackPanel Orientation="Horizontal">
                <Border Margin=".5" >
                    <Button Margin="6" Style="{StaticResource StaticLinkButton}" >
                        <TextBlock Text="Qty OH" />
                    </Button>
                </Border>
                <Border Margin=".5" >
                    <Button Margin="6" Style="{StaticResource StaticLinkButton}" >
                        <TextBlock Text="Ord Qty" />
                    </Button>
                </Border>
                <Border Margin=".5" >
                    <Button Margin="6" Style="{StaticResource StaticLinkButton}" >
                        <TextBlock Text="Ret Qty" />
                    </Button>
                </Border>
                <Border Margin=".5" >
                    <Button Margin="6" Style="{StaticResource StaticLinkButton}" >
                        <TextBlock Text="Rec Qty" />
                    </Button>
                </Border>
                <Border Margin=".5" >
                    <Button Margin="6" Style="{StaticResource StaticLinkButton}" KeyboardNavigation.IsTabStop="False" >
                        <TextBlock MinWidth="90" MinHeight="27" Text="Quantity" />
                    </Button>
                </Border>
            </StackPanel>
            <Border Margin=".5" Background="{StaticResource LightBlueBackground}">
                <Button ="Part Type" KeyboardNavigation.IsTabStop="False"/>
            </Border>
        </StackPanel>
    </DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>

I've got a design requirement for a pretty strange grid... looks like this:

grid http://img219.imageshack.us/img219/3444/columns.png

I need to be able to sort on the headers in both rows... so if they click "Description", it sorts by description. If they click "Status", it sorts by status. Using templates on the column headers, I could achieve the look, but I can't seem to figure out a way to get each individual unit to function as a distinct header. How exactly can I go about this?

EDIT: To create the column headers, I did this:

<StackPanel Orientation="Vertical" >
     <Border Margin=".5" Background="{StaticResource DarkBlueBackground}" >
           <Button Margin="6" Style="{StaticResource StaticLinkButton}"">
                <TextBlock HorizontalAlignment="Left" Text="Part Number" MinWidth="40"  />
           </Button>
     </Border>

     <Border Margin=".5" Background="{StaticResource LightBlueBackground}" MinWidth="120">
            <Button  Margin="6" Style="{StaticResource StaticLinkButton}" Content="Part Type"/>
     </Border>
 </StackPanel>

, which works for the most part. The problem is that, when I have a particularly long data item (like a long 'part type'), it stretches out the column, but the border around my "Part Type" header doesn't stretch. I tried using a grid instead of a stack panel as well, but with the same results. Is there a relatively simple way to get the borders/buttons/textblocks in the headers to stretch out to the full width of the column?

Edit 2
Showing header template code per request:

<DataGridTemplateColumn.HeaderTemplate>
    <DataTemplate>
        <StackPanel Orientation="Vertical" >
            <StackPanel Orientation="Horizontal">
                <Border Margin=".5" >
                    <Button Margin="6" Style="{StaticResource StaticLinkButton}" >
                        <TextBlock Text="Qty OH" />
                    </Button>
                </Border>
                <Border Margin=".5" >
                    <Button Margin="6" Style="{StaticResource StaticLinkButton}" >
                        <TextBlock Text="Ord Qty" />
                    </Button>
                </Border>
                <Border Margin=".5" >
                    <Button Margin="6" Style="{StaticResource StaticLinkButton}" >
                        <TextBlock Text="Ret Qty" />
                    </Button>
                </Border>
                <Border Margin=".5" >
                    <Button Margin="6" Style="{StaticResource StaticLinkButton}" >
                        <TextBlock Text="Rec Qty" />
                    </Button>
                </Border>
                <Border Margin=".5" >
                    <Button Margin="6" Style="{StaticResource StaticLinkButton}" KeyboardNavigation.IsTabStop="False" >
                        <TextBlock MinWidth="90" MinHeight="27" Text="Quantity" />
                    </Button>
                </Border>
            </StackPanel>
            <Border Margin=".5" Background="{StaticResource LightBlueBackground}">
                <Button ="Part Type" KeyboardNavigation.IsTabStop="False"/>
            </Border>
        </StackPanel>
    </DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>

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

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

发布评论

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

评论(1

酒中人 2024-11-16 10:42:39

您可以将 TextBlock 或“不可见”按钮中的任何内容包装起来,并处理它们的点击或在某些主处理程序中检查 e.OriginalSource 是什么。 (命令也可能是一个选项)

要使按钮“不可见”,您可以应用一个简单的样式,使按钮仅显示其内容:

<Style x:Key="LooklessButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <ContentPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

You can wrap the TextBlocks or whatever you have in "invisible" buttons and handle their clicks or check in some main handler what the e.OriginalSource is. (Commands may also be an option)

To make buttons "invisible" you can apply a simple style which makes the button only show its contents:

<Style x:Key="LooklessButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <ContentPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文