2

I have DataGrid with customized column like that

<DataGridTemplateColumn Width="2*">
    <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <Grid>
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="*"></ColumnDefinition>
                     <ColumnDefinition Width="*"></ColumnDefinition>
             </Grid.ColumnDefinitions>
             <TextBlock Grid.Column="0" Text="{Binding SomeProperty}"/> 
             <TextBlock Grid.Column="0" Text="{Binding OtherProperty}"/>     
             </Grid>
         </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
 </DataGridTemplateColumn>
 <DataGridTextColumn Width="*" Header="Const" Binding="{Binding ConstantValue}" />

As you can see, first column has double width of second column and contains two texblocks. So I need make header consists from two words. Every word above corresponding TexBlock. And I try to use Grid with two columns (because I have textblocks in two columns) as HeaderTemplate. I try to set HeaderStyle like that

<DataGridTemplateColumn.HeaderStyle>
     <Style TargetType="{x:Type DataGridColumnHeader}">
            <Style.Setters>
                  <Setter Property="ContentTemplate">
                         <Setter.Value>
                              <DataTemplate>
                                    <Grid>
                                         <Grid.ColumnDefinitions>
                                              <ColumnDefinition Width="*"></ColumnDefinition>
                                              <ColumnDefinition Width="*"></ColumnDefinition>
                                         </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Column="0" HorizontalAlignment="Center">Header1</TextBlock>
                                    <TextBlock Grid.Column="1" HorizontalAlignment="Center">Header2</TextBlock>
                                    </Grid>
                              </DataTemplate>
                         </Setter.Value>
                   </Setter>
           </Style.Setters>
     </Style>

and I try to use HeaderTemplate like that

<DataGridTemplateColumn.HeaderTemplate>
     <DataTemplate>
          <Grid>
                <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="*"></ColumnDefinition>
                     <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" HorizontalAlignment="Center">Header1</TextBlock>
                <TextBlock Grid.Column="1" HorizontalAlignment="Center">Header2</TextBlock>
          </Grid>
     </DataTemplate>
 </DataGridTemplateColumn.HeaderTemplate>

But in both cases I have the same wrong result. Namely. Header1 and Header2 placed togeter without any spaces like Header1Header2. And looks like Header1Header2 has HorizontalAligment="Left" relatively whole header. I.e.

column border-->|Header1Header2___________|<--column border

So Header1 and Header2 do not placed above corresponding TextBlocks in DataGrid. How can I solve my problem? How should I set HeaderTemplate to take what I want?

1 Answer 1

1

Would customizing the ControlTemplate work for you?

<DataGridTemplateColumn.HeaderStyle>
    <Style TargetType="{x:Type DataGridColumnHeader}">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0" HorizontalAlignment="Center">Header1</TextBlock>
                            <TextBlock Grid.Column="1" HorizontalAlignment="Center">Header2</TextBlock>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>
</DataGridTemplateColumn.HeaderStyle>

Your other option is to create your own DataGridColumnHeadersPresenter which will hint the available size to the headers. By default, headers' ContentPresenters won't be told the available size and will calculate the width from their content (DataTemplate).

In your case, the width is not set explicitly (e.g. <Grid Width="200">) so it'll simply be the combined size of the TextBoxes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.