简体   繁体   中英

Column Headers Offset From Data in Wpf Datagrid

Good morning

I have a Wpf datagrid that is displaying an observable collection of a custom type

I group the data using a collection view source in XAML on two seperate properties, and I have styled the groups to display as expanders.

For clarity, as there is a lot of data I feel I have to use margins and spacing otherwise things look very cluttered.

My problem is that with two levels of hierarchical expanders the column data is now substantially offset from the column headers meaning that they do not properly line up.

I have tried several thing, like setting the margin of the column headers and the width (both actual and normal). However all of my attempts end up resizing the whole column so that the offset stays the same but the columns move.

so my question:

How can I change the visible width or offset of a column header to ensure that the headers line up with the data

  • Visual Studio 2012
  • Wpf
  • C#
  • DataGrid

EDIT This is what I mean

偏移列

EDIT 2 - MY Xaml for Grouping

<!--  Style for groups at top level.  -->
<GroupStyle>
    <GroupStyle.ContainerStyle>
        <Style TargetType="{x:Type GroupItem}">
            <Setter Property="Margin" Value="0" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander Margin="5,10,5,5"
                                  BorderBrush="{StaticResource BlackBrush}"
                                  BorderThickness="1"
                                  Header="{Binding Name}"
                                  IsExpanded="True">

                            <Expander.Template>
                                <!--  The basic expander  -->
                                <ControlTemplate TargetType="{x:Type Expander}">
                                    <!--  Put a border around the expander  -->
                                    <Border Background="{Binding Path=Name,
                                                                 Converter={StaticResource ColourConverter}}"
                                            BorderBrush="{StaticResource GreyBrush}"
                                            BorderThickness="2"
                                            CornerRadius="3">

                                        <!--  Use a dock panel so that the toggle button is docked to the top and the content is docked to the bottom  -->
                                        <DockPanel Margin="0">

                                            <!--  Add the toggle button  -->
                                            <ToggleButton x:Name="ExpanderButton"
                                                          Margin="0"
                                                          Content="{TemplateBinding Header}"
                                                          DockPanel.Dock="Top"
                                                          FontSize="14"
                                                          FontWeight="Bold"
                                                          Foreground="{StaticResource BlackBrush}"
                                                          IsChecked="{Binding Path=IsExpanded,
                                                                              RelativeSource={RelativeSource TemplatedParent}}"
                                                          OverridesDefaultStyle="True"
                                                          Template="{StaticResource AnimatedExpanderButton}" />

                                            <ContentPresenter x:Name="ExpanderContent"
                                                              Margin="5"
                                                              ContentSource="Content"
                                                              DockPanel.Dock="Bottom"
                                                              Visibility="{Binding ElementName=ExpanderButton,
                                                                                   Path=IsChecked,
                                                                                   Converter={StaticResource VisibilityConverter}}" />
                                        </DockPanel>
                                    </Border>
                                </ControlTemplate>
                            </Expander.Template>
                            <ItemsPresenter />
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </GroupStyle.ContainerStyle>
</GroupStyle>

<!--  Style for groups under the top level.  -->
<GroupStyle>
    <GroupStyle.ContainerStyle>
        <Style TargetType="{x:Type GroupItem}">

            <Setter Property="Margin" Value="0,0,0,5" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander Margin="5"
                                  Background="{Binding Path=Name,
                                                       Converter={StaticResource ColourConverter}}"
                                  IsExpanded="True"
                                  Visibility="{Binding Items[0].IsSelectedInSidebar,
                                                       Converter={StaticResource VisibilityConverter}}">

                            <Expander.Template>
                                <!--  The basic expander  -->
                                <ControlTemplate TargetType="{x:Type Expander}">
                                    <!--  Put a border around the expander  -->
                                    <Border Background="{Binding Path=Name,
                                                                 Converter={StaticResource ColourConverter}}"
                                            BorderBrush="{StaticResource GreyBrush}"
                                            BorderThickness="2"
                                            CornerRadius="3">

                                        <!--  Use a dock panel so that the toggle button is docked to the top and the content is docked to the bottom  -->
                                        <DockPanel Margin="0">

                                            <!--  Add the toggle button  -->
                                            <ToggleButton x:Name="ExpanderButton"
                                                          Content="{Binding Path=Name}"
                                                          DockPanel.Dock="Top"
                                                          FontSize="12"
                                                          IsChecked="{Binding Path=IsExpanded,
                                                                              RelativeSource={RelativeSource TemplatedParent}}"
                                                          OverridesDefaultStyle="True"
                                                          Template="{StaticResource AnimatedExpanderButton}" />

                                            <ContentPresenter x:Name="ExpanderContent"
                                                              Margin="5"
                                                              ContentSource="Content"
                                                              DockPanel.Dock="Bottom"
                                                              Visibility="{Binding ElementName=ExpanderButton,
                                                                                   Path=IsChecked,
                                                                                   Converter={StaticResource VisibilityConverter}}" />
                                        </DockPanel>
                                    </Border>
                                </ControlTemplate>
                            </Expander.Template>
                            <Expander.Content>
                                <Border BorderBrush="{StaticResource BlackBrush}" BorderThickness="1">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>
                                        <ItemsPresenter Grid.Row="0" Margin="0" />

                                        <Border Grid.Row="1"
                                                Margin="0,10,0,0"
                                                BorderBrush="{StaticResource BlackBrush}"
                                                BorderThickness="0,1,0,0"
                                                Visibility="{Binding Data.SettingRepository.MainDataSummaryVisible,
                                                                     Source={StaticResource BindingProxy},
                                                                     Converter={StaticResource VisibilityConverter}}">
                                            <Grid Background="{StaticResource WhiteBrush}">
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="Auto" />
                                                    <RowDefinition Height="Auto" />
                                                    <RowDefinition Height="Auto" />
                                                </Grid.RowDefinitions>

                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition Width="Auto" />
                                                </Grid.ColumnDefinitions>

                                                <Grid Grid.Row="0" Grid.ColumnSpan="6">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition Width="Auto" />
                                                    </Grid.ColumnDefinitions>

                                                    <TextBlock Grid.Column="0"
                                                               Margin="5"
                                                               FontWeight="Bold"
                                                               Text="{Binding Path=Items[0].Option1Title}"
                                                               Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                    Source={StaticResource BindingProxy},
                                                                                    Converter={StaticResource VisibilityConverter}}" />
                                                    <TextBlock Grid.Column="1"
                                                               Margin="5"
                                                               Text="{Binding Path=Items[0].Option1Data,
                                                                              Mode=OneWay}"
                                                               Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                    Source={StaticResource BindingProxy},
                                                                                    Converter={StaticResource VisibilityConverter}}" />

                                                    <TextBlock Grid.Column="2"
                                                               Margin="5"
                                                               FontWeight="Bold"
                                                               Text="{Binding Path=Items[0].Option2Title}"
                                                               Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                    Source={StaticResource BindingProxy},
                                                                                    Converter={StaticResource VisibilityConverter}}" />
                                                    <TextBlock Grid.Column="3"
                                                               Margin="5"
                                                               Text="{Binding Path=Items[0].Option2Data,
                                                                              Mode=OneWay}"
                                                               Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                    Source={StaticResource BindingProxy},
                                                                                    Converter={StaticResource VisibilityConverter}}" />
                                                    <TextBlock Grid.Column="4"
                                                               Margin="5"
                                                               FontWeight="Bold"
                                                               Text="{Binding Path=Items[0].Option3Title}"
                                                               Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                    Source={StaticResource BindingProxy},
                                                                                    Converter={StaticResource VisibilityConverter}}" />
                                                    <TextBlock Grid.Column="5"
                                                               Margin="5"
                                                               Text="{Binding Path=Items[0].Option3Data,
                                                                              Mode=OneWay}" />
                                                    <TextBlock Grid.Column="6"
                                                               Margin="5"
                                                               FontWeight="Bold"
                                                               Text="{Binding Path=Items[0].Option4Title}"
                                                               Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                    Source={StaticResource BindingProxy},
                                                                                    Converter={StaticResource VisibilityConverter}}" />
                                                    <TextBlock Grid.Column="7"
                                                               Margin="5"
                                                               Text="{Binding Path=Items[0].Option4Data,
                                                                              Mode=OneWay}"
                                                               Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                    Source={StaticResource BindingProxy},
                                                                                    Converter={StaticResource VisibilityConverter}}" />

                                                    <TextBlock Grid.Column="8"
                                                               Margin="5"
                                                               FontWeight="Bold"
                                                               Text="{x:Static languages:Strings.SampleIsAnnealedColumnHeader}" />
                                                    <CheckBox Grid.Column="9"
                                                              Margin="3,5,5,5"
                                                              IsChecked="{Binding Path=Items[0].SampleIsAnnealed,
                                                                                  Mode=OneWay}"
                                                              IsHitTestVisible="False"
                                                              Style="{StaticResource FandFCheckBox}" />
                                                </Grid>

                                                <!--  The mean Match temperature  -->
                                                <TextBlock Grid.Row="1"
                                                           Grid.Column="0"
                                                           Margin="5"
                                                           FontWeight="Bold"
                                                           Text="{x:Static languages:Strings.MeanSampleMatchTemperatureTitle}" />
                                                <TextBlock Grid.Row="1"
                                                           Grid.Column="1"
                                                           Margin="5"
                                                           Text="{Binding Path=Items[0].SampleMeanMatchTemperature,
                                                                          Mode=OneWay,
                                                                          StringFormat=\{0:N2\}}" />

                                                <!--  The match temperature range  -->
                                                <TextBlock Grid.Row="1"
                                                           Grid.Column="2"
                                                           Margin="5"
                                                           FontWeight="Bold"
                                                           Text="{x:Static languages:Strings.SampleTemperatureRangeTitle}" />
                                                <TextBlock Grid.Row="1"
                                                           Grid.Column="3"
                                                           Margin="5"
                                                           Text="{Binding Path=Items[0].SampleMatchTemperatureRange}" />

                                                <!--  The match temperature standard deviation  -->
                                                <TextBlock Grid.Row="1"
                                                           Grid.Column="4"
                                                           Margin="5"
                                                           FontWeight="Bold"
                                                           Text="{x:Static languages:Strings.SampleTemperatureStandardDeviationTitle}" />
                                                <TextBlock Grid.Row="1"
                                                           Grid.Column="5"
                                                           Margin="5"
                                                           Text="{Binding Path=Items[0].SampleMatchTemperatureStandardDeviation,
                                                                          Mode=OneWay,
                                                                          StringFormat=\{0:N3\}}" />

                                                <!--  The mean refractive index  -->
                                                <TextBlock Grid.Row="2"
                                                           Grid.Column="0"
                                                           Margin="5"
                                                           FontWeight="Bold"
                                                           Text="{x:Static languages:Strings.SampleMeanRefractiveIndexTitle}" />
                                                <TextBlock Grid.Row="2"
                                                           Grid.Column="1"
                                                           Margin="5"
                                                           Text="{Binding Path=Items[0].SampleMeanRefractiveIndex,
                                                                          Mode=OneWay,
                                                                          StringFormat=\{0:N5\}}" />

                                                <!--  The refractive index range  -->
                                                <TextBlock Grid.Row="2"
                                                           Grid.Column="2"
                                                           Margin="5"
                                                           FontWeight="Bold"
                                                           Text="{x:Static languages:Strings.SampleRIRangeTitle}" />
                                                <TextBlock Grid.Row="2"
                                                           Grid.Column="3"
                                                           Margin="5"
                                                           Text="{Binding Path=Items[0].SampleRefractiveIndexRange}" />

                                                <!--  The refractive index standard deviation  -->
                                                <TextBlock Grid.Row="2"
                                                           Grid.Column="4"
                                                           Margin="5"
                                                           FontWeight="Bold"
                                                           Text="{x:Static languages:Strings.SampleRIStandardDeviationTitle}" />
                                                <TextBlock Grid.Row="2"
                                                           Grid.Column="5"
                                                           Margin="5"
                                                           Text="{Binding Path=Items[0].SampleRefractiveIndexStandardDeviation,
                                                                          Mode=OneWay,
                                                                          StringFormat=\{0:N7\}}" />
                                            </Grid>
                                        </Border>
                                    </Grid>
                                </Border>
                            </Expander.Content>
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </GroupStyle.ContainerStyle>
</GroupStyle>

You can set the ColumnHeaderStyle and there set a RenderTransform that moves the headers to the right.

<DataGrid.ColumnHeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="RenderTransform">
            <Setter.Value>
                //change the X value accordingly
                <TranslateTransform X="100"></TranslateTransform>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.ColumnHeaderStyle>

EDIT 2:

As you mentioned, doing this will result in a small gap. To remove it you should set the left margin of the first column to a negative value, which stretches the header of this column to the left. You can do it like this:

<DataGridTemplateColumn.HeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        //change the margin accordingly
        <Setter Property="Margin" Value="-100 0 0 0" />
        <Setter Property="RenderTransform">
            <Setter.Value>
                //change the X value accordingly
                <TranslateTransform X="100"></TranslateTransform>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridTemplateColumn.HeaderStyle>

You have to set the RenderTransform here again, because this style overwrites the general ColumnHeaderStyle . To remove duplication you can add the render transfrom as a resource.

EDIT:

I just saw that you have a few margins on your Expanders and ContentPresenters . If you change them so that you have 0 margin to the left, it would align the content more to the left and reduce the alignment difference.

You would then need less offset on the RenderTransform .

Some examples of your code to illustrate what I mean:

<Expander Margin="5,10,5,5"
<ContentPresenter x:Name="ExpanderContent" Margin="5"

If you change it to

<Expander Margin="0,10,5,5"
<ContentPresenter x:Name="ExpanderContent" Margin="0 5 5 5"

the columns move more to the left.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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