简体   繁体   English

WPF中Datagrid列标题上的Shift图标

[英]Shift icon on Datagrid column header in WPF

I have a datagrid for which I want to support excel like filter functionality. 我有一个要支持excel之类的数据网格,例如过滤器功能。 Here is the screenshot of the column header: 这是列标题的屏幕截图: 在此处输入图片说明

I have added a filter icon next to column header. 我在列标题旁边添加了一个过滤器图标。 The problem is that this icon should be shifted to the right most side of the column. 问题在于此图标应移至该列的最右侧。 Just where the empty circle is. 只是空圈在哪里。 The column can be resized by user using dragging with mouse and therefore the icon should stick to the right end of the column when user increases or decreases the width. 用户可以使用鼠标拖动来调整列的大小,因此,当用户增加或减少宽度时,图标应停留在列的右端。 Here is the code: 这是代码:

<DataGridHyperlinkColumn Binding="{Binding PackageName}"  MinWidth="250" IsReadOnly="True" >
                        <DataGridHyperlinkColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Package Name"/>
                                <Button Name="PackageNameFilter" Click="PackageNameFilter_Click" HorizontalAlignment="Right">
                                    <Button.Template>
                                        <ControlTemplate>
                                            <Image Source="/Resources/filter.png" Width="10" Height="10"/>
                                        </ControlTemplate>
                                    </Button.Template>
                                </Button>
                            </StackPanel>
                        </DataGridHyperlinkColumn.Header>

How can I achieve this? 我该如何实现?

I am writing this on the fly. 我正在写这篇文章。 It will give you an idea to overcome the issue. 它会给您一个解决该问题的想法。 You can achieve this by either using Grid or DockPanel . 您可以使用GridDockPanel来实现。 Here is the one using DockPanel 这是使用DockPanel那个

<DataGridHyperlinkColumn.Header>
    <DockPanel>
         <TextBlock Text="Package Name"/>
         <Button DockPanel.Dock="Right" />
    </DockPanel>
</DataGridHyperlinkColumn.Header>

EDIT: 编辑:

Update based on comments, you have to stretch the width of the column header to occupy the full width as mentioned by @grek40 根据注释进行更新,您必须拉伸列标题的宽度以占据@ grek40提到的完整宽度

<DataGridTextColumn.HeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</DataGridTextColumn.HeaderStyle>

Hope that helps. 希望能有所帮助。

You need to ensure that the header content is actually using the size of the header. 您需要确保标题内容实际上是使用标题的大小。 I'm using Dockpanel similar to Gopichandar's answer and a HeaderStyle definition. 我正在使用类似于Gopichandar的答案和HeaderStyle定义的Dockpanel。

<DataGridHyperlinkColumn Binding="{Binding Test}" Width="*">
    <DataGridHyperlinkColumn.Header>
        <DockPanel LastChildFill="False">
            <TextBlock Text="Header Text"/>
            <Button DockPanel.Dock="Right" Content="Filter"/>
        </DockPanel>
    </DataGridHyperlinkColumn.Header>
    <DataGridHyperlinkColumn.HeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </DataGridHyperlinkColumn.HeaderStyle>
</DataGridHyperlinkColumn>

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

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