0

I made a class inheriting from datagridcolumnheader. I styled it (using for now the default style of the original datagridcolumnheader) But I dont know how to force my columns to use this customDatagridcolumnHeader.

I tried this :

<DataGridTextColumn Binding="{Binding Donateur}" Width="*">
                        <DataGridTextColumn.Header>
                            <local:CustomDatagridColumnHeader/>
                        </DataGridTextColumn.Header>          
                    </DataGridTextColumn>

It's not working, and I understand why. How to do something like that ? I tried to modify the default DatagridColumnHeadersPresenter, but it has only two pars, the PART_FillerColumnHeader (which I CAN replace) and the itemsPresenter. The purpose is to add property, function and events on the new class (Just trying to have textboxes for filter, but not for all columns, and not for all datagrid). I "think" I can find a way to make it by inheriting directly the datagrid, and then finding via the tree the textboxes, but that's not very practical for all the things I want to do (like hiding the textbox for one column)

EDIT : Here is the future template

<Style TargetType="local:RafDatagridColumnHeader">
    <Style.Resources>
        <local:IsNotNothingConverter x:Key="IsNotNothingConverter"/>
        <local:IsNotNothingToVisibilityConverter x:Key="IsNotNothingToVisibilityConverter"/>
    </Style.Resources>
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock TextWrapping="Wrap" Text="{Binding}" VerticalAlignment="Center"></TextBlock>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:RafDatagridColumnHeader">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Grid Grid.Row="0" Visibility="{Binding Column, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:RafDatagridColumnHeader}, Converter={StaticResource IsNotNothingToVisibilityConverter}}">
                        <TextBox HorizontalAlignment="Stretch" Margin="3"/>
                    </Grid>

                    <theme:DataGridHeaderBorder Grid.Row="1" SortDirection="{TemplateBinding SortDirection}"
                                IsHovered="{TemplateBinding IsMouseOver}"
                                IsPressed="{TemplateBinding IsPressed}"
                                IsClickable="{TemplateBinding CanUserSort}"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding ="{TemplateBinding Padding}"
                                SeparatorVisibility="{TemplateBinding SeparatorVisibility}"
                                SeparatorBrush="{TemplateBinding SeparatorBrush}" VerticalAlignment="Stretch">


                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            RecognizesAccessKey="True" />

                    </theme:DataGridHeaderBorder>

                    <Thumb x:Name="PART_LeftHeaderGripper"  Grid.Row="1"
                        HorizontalAlignment="Left"
                        Style="{StaticResource ColumnHeaderGripperStyle}"/>
                    <Thumb x:Name="PART_RightHeaderGripper"  Grid.Row="1"
                        HorizontalAlignment="Right"
                        Style="{StaticResource ColumnHeaderGripperStyle}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

If I modify the control DataTemplate instead of the templaet itself, I will lose all the good stuff of the columnheader (sorting, sorting glyph, thumbs to change width, reordering...) Well, in fact, I will not, but the textbox will be INSIDE the header, and the sort glyph will be on top... By the way I did it, it's nicer and more readable

Epervier 666
  • 97
  • 1
  • 8
  • You can try to inherit the DataGridTextColumn, and change the header inside it, then use instead of the regular one. – mZm Feb 24 '21 at 09:44
  • Also I think that inside you can put almost everything, it doesn't have to be a datagridcolumnheader. You can make a user control or something and use it. – mZm Feb 24 '21 at 09:50
  • I edited the quesiton to explain why I don't want to juste put another control in the header, but use inherited datagridcolumnheader. As to inherits DatagridTextColumn, will this allow me to replace header with my own ? how so ? (I'm french by the way, excuse my english mistakes) – Epervier 666 Feb 24 '21 at 10:15
  • If I put my first code, I have an error related to "element can't be its own parent" ,which is logical, because I put a customdatagridcolumnheader inside a datagridcolumnheader. What I want is use my custom datagridcolumnheader in place of the default datagridcolumnheader (and I need a custom for adding property, events and functions, orelse, i would just style the default one) – Epervier 666 Feb 24 '21 at 12:01
  • @Epervier666: How exactly is your `CustomDatagridColumnHeader` defined? – mm8 Feb 24 '21 at 16:21

0 Answers0