简体   繁体   English

DataGridColumnHeader 中的 UserControl 不拉伸

[英]UserControl in DataGridColumnHeader does not stretch

I'm trying to create a custom header for my DataGrid to be able to filter in a powerful way.我正在尝试为我的 DataGrid 创建一个自定义标题,以便能够以强大的方式进行过滤。 Everything works like I want to except the fact that the UserControl does not stretch with the column.一切都像我想要的那样,除了 UserControl 不随列伸展。 I've colored the background of the UserControl red to be able to se it more clearly in the image.我将 UserControl 的背景涂成红色,以便能够在图像中更清楚地看到它。

在此处输入图片说明

I've been looking for an answer here and elsewhere, but all I've found suggests that I should not set the width explicitly in the UserControl, and I've checked that.我一直在这里和其他地方寻找答案,但我发现的所有内容都表明我不应该在 UserControl 中明确设置宽度,我已经检查过了。

I've tried adding a grid in the header and then put the usercontrol in the header, but that does not help.我试过在标题中添加一个网格,然后将用户控件放在标题中,但这无济于事。

Below is how I add the UserControl to one of the headers:下面是我如何将 UserControl 添加到标题之一:

                <DataGridTextColumn Binding="{Binding Path=Description}">
                    <DataGridTextColumn.Header>
                            <views:FilterControl Header="Description"  FilterChangedEvent="Filtering_Changed" PropertyPath="Description"  /> 
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

This is the UserControl xaml in its entirety:这是完整的 UserControl xaml:

 <UserControl x:Class="White.Db.ContentOrderDb.Views.FilterControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:White.Db.ContentOrderDb.Views"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="120"   HorizontalAlignment="Stretch">
<UserControl.Resources>
    <Style TargetType="FrameworkElement" x:Key="IsEnabledStyle">
        <Setter Property="IsEnabled" Value="False"/>
        <Setter Property="OpacityMask" Value="Black"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=FilterActive}" Value="True" >
                <Setter Property="IsEnabled" Value="True"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<Grid Background="White">
    <TextBox Style="{StaticResource IsEnabledStyle}" TextChanged="SearchPatternText_Changed" Height="23" Margin="3,50,3,0" TextWrapping="Wrap" Text="{Binding Path=SearchPattern,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top">
        <TextBox.BorderBrush>
            <LinearGradientBrush EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0">
                <GradientStop Color="#FFABADB3" Offset="0.05"/>
                <GradientStop Color="#FFE2E3EA" Offset="0.07"/>
                <GradientStop Color="#FFB4B4B4" Offset="1"/>
            </LinearGradientBrush>
        </TextBox.BorderBrush>
    </TextBox>
    <Label Content="{Binding Path=Header}" VerticalAlignment="Top" Height="30" Background="gray"  Foreground="White" BorderBrush="#FFB6B6B6" BorderThickness="1"/>
    <CheckBox Unchecked="FilterDeActivated" Checked="FilterActivated" IsChecked="{Binding Path=FilterActive}" Content="Active" HorizontalAlignment="Left" Margin="3,32,0,0" VerticalAlignment="Top" BorderBrush="#FFB4B4B4"/>
    <ComboBox  SelectionChanged="ComboBox_SelectionChanged" Style="{StaticResource IsEnabledStyle}" Text="{Binding Path=ComboBoxText}" IsReadOnly="True" IsEditable="True" ItemsSource="{Binding Path=ComboBoxItems}" Margin="3,75,3,0" VerticalAlignment="Top">
        <ComboBox.BorderBrush>
            <LinearGradientBrush EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0">
                <GradientStop Color="#FFABADB3" Offset="0.05"/>
                <GradientStop Color="#FFE2E3EA" Offset="0.07"/>
                <GradientStop Color="#FFB4B4B4" Offset="1"/>
            </LinearGradientBrush>
        </ComboBox.BorderBrush>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Path=Content}" IsChecked="{Binding Path=IsChecked}" Checked="CheckBox_CheckedChanged" Unchecked="CheckBox_CheckedChanged" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Grid>

Any thoughts?有什么想法吗? I've not seen any examples where people add UserControls to headers, is this the wrong way?我还没有看到人们将 UserControls 添加到标题的任何示例,这是错误的方式吗? All I've seen is templates and styles being used.我所看到的只是正在使用的模板和样式。 I've added all the logic for filtering to the usercontrol and its viewmodel since I want to reuse it, so I dont see how a template can help me.因为我想重用它,所以我已经将所有用于过滤的逻辑添加到用户控件及其视图模型中,所以我不知道模板如何帮助我。

Thanks!谢谢!

/Erik /埃里克

If you set the HorizontalAlignment property of your control to Stretch , you tell that control to try to take up as much of available horizontal space as possible.如果您将控件的HorizontalAlignment属性设置为Stretch ,则您告诉该控件尝试占用尽可能多的可用水平空间。 The problem here is that the container which hosts your control (it's a DataGridColumnHeader control in this case) tries to keep it's content to the left and take up as little horizontal space as possible - this is because it's HorizontalContentAlignment property is by default set to Left .这里的问题是承载您的控件的容器(在这种情况下是DataGridColumnHeader控件)试图将其内容保持在左侧并占用尽可能少的水平空间 - 这是因为它的HorizontalContentAlignment属性默认设置为Left . So in order to allow your control to stretch freely you need to also set that property to Stretch .因此,为了让您的控件能够自由拉伸,您还需要将该属性设置为Stretch The easiest way of doing that is to utilize the DataGridColumn.HeaderStyle property:最简单的方法是利用DataGridColumn.HeaderStyle属性:

<DataGridTextColumn Binding="{Binding Path=Description}">
    <DataGridTextColumn.Header>
        <views:FilterControl Header="Description" FilterChangedEvent="Filtering_Changed" PropertyPath="Description"  /> 
    </DataGridTextColumn.Header>
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>

You may also need to play around with other properties of DataGridColumnHeader to get the desired result - I've noticed that by default it's content is padded, and setting neither Padding nor Margin properties helps, but setting the Background property to Transparent (or any other for that matter) gets rid of that mysterious padding.您可能还需要使用DataGridColumnHeader其他属性来获得所需的结果 - 我注意到默认情况下它的内容是填充的,并且设置PaddingMargin属性都没有帮助,但是将Background属性设置为Transparent (或任何其他就此而言)摆脱了那个神秘的填充。

1. It is possible to change the style of all column header with applying style : 1. 可以通过应用样式更改所有列标题的样式:

<Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                <Grid>
                    <Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
                        <ContentPresenter HorizontalAlignment="Stretch" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Themes:DataGridHeaderBorder>
                    <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                    <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The important modification to the "copied template" is :对“复制模板”的重要修改是:

 <ContentPresenter HorizontalAlignment="Stretch" ... >

2. Then use the style : 2.然后使用样式:

<DataGrid HorizontalContentAlignment="Stretch" x:Name="datagrid1" 
          ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}">

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

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