Wpf Datagrid中数据的列标题偏移 [英] Column Headers Offset From Data in Wpf Datagrid

查看:61
本文介绍了Wpf Datagrid中数据的列标题偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好

我有一个Wpf数据网格,它正在显示可观察到的自定义类型的集合

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

我使用XAML中的集合视图源在两个单独的属性上对数据进行分组,并设置了样式以显示为扩展器.

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.

所以我的问题:

如何更改列标题的可见宽度或偏移量,以确保标题与数据对齐

  • Visual Studio 2012
  • Wpf
  • C#
  • DataGrid

编辑,这就是我的意思

编辑2-MY Xaml用于分组

<!--  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>

推荐答案

您可以设置 ColumnHeaderStyle ,然后在其中设置 RenderTransform ,以将标题向右移动./p>

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>

正如您提到的,这样做会导致很小的差距.要删除它,您应该将第一列的左边距设置为负值,这会将此列的标题向左扩展.您可以这样做:

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>

您必须在此处再次设置 RenderTransform ,因为此样式将覆盖常规的 ColumnHeaderStyle .要删除重复项,您可以将渲染Transfrom添加为资源.

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.

我刚刚看到您在 Expanders ContentPresenters 上有一些边距.如果更改它们以使左边距为0,它将使内容更多地向左对齐并减小对齐差异.

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.

然后,您需要在 RenderTransform 上减少偏移量.

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"

如果将其更改为

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

列向左移动更多.

这篇关于Wpf Datagrid中数据的列标题偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆