DataGrid当列宽超出当前宽度时,没有数据也恒有滚动条

时间:2021-04-04 23:13:30

附件是DataGrid支持滚动条的文件。 具体使用如下:

1)DataGrid使用控件模板
<Setter Property="Template" Value="{DynamicResource grdStudyListDataGridControlTemplate}">

   <ControlTemplate x:Key="grdStudyListDataGridControlTemplate" TargetType="{x:Type DataGrid}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True" CornerRadius="9">
                    <ScrollViewer x:Name="DG_ScrollViewer" AutomationProperties.AutomationId="PADG_ScrollViewer" Focusable="false">
                        <ScrollViewer.Template>
                            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <!--<Button Command="{x:Static DataGrid.SelectAllCommand}" Focusable="false" 
                                                    Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}"
                                                    Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="Collapsed" Margin="0"/>-->
                                    <ScrollViewer x:Name="scvColHeaders" AutomationProperties.AutomationId="scvPAColHeaders" Grid.Column="1"  Margin="0,0,0,0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" 
                                                          HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Disabled" Template="{DynamicResource ScrollViewerControlTemplate1}" 
                                                          >
                                        <ScrollViewer.Resources>
                                            <ControlTemplate x:Key="ScrollViewerControlTemplate1" TargetType="{x:Type ScrollViewer}">
                                                <Grid x:Name="Grid" AutomationProperties.AutomationId="PAGrid" Background="{TemplateBinding Background}">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*"/>
                                                        <ColumnDefinition Width="Auto"/>
                                                    </Grid.ColumnDefinitions>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="*"/>
                                                        <RowDefinition Height="Auto"/>
                                                    </Grid.RowDefinitions>
                                                    <Rectangle x:Name="Corner" AutomationProperties.AutomationId="PACorner" Grid.Column="1" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Grid.Row="1"/>
                                                    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" AutomationProperties.AutomationId="PAPART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False"
                                                                                    CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"
                                                                                    Grid.Column="0" Margin="{TemplateBinding Padding}" Grid.Row="0"/>
                                                    <ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="1" 
                                                                       Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Grid.Row="0" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" 
                                                                       Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"
                                                               ContextMenu="{x:Null}"/>
                                                    <ScrollBar x:Name="PART_HorizontalScrollBar" AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow" Grid.Column="0" 
                                                               SmallChange="20"        
                                                               Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Orientation="Horizontal" Grid.Row="1" 
                                                                       Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" 
                                                                       Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"
                                                               ContextMenu="{x:Null}"/>
                                                </Grid>
                                            </ControlTemplate>
                                        </ScrollViewer.Resources>
                                        <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" AutomationProperties.AutomationId="PAPART_ColumnHeadersPresenter"  VirtualizingStackPanel.IsVirtualizing="False" VirtualizingStackPanel.VirtualizationMode="Standard" 
                                                                                Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                                                                Height="Auto" Width="Auto" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" 
                                                                                ItemsPanel="{DynamicResource ItemsPanelTemplate1}" Style="{DynamicResource DataGridColumnHeadersPresenterStyle1}"
                                                                                >
                                            <DataGridColumnHeadersPresenter.Resources>
                                                <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
                                                    <DataGridCellsPanel IsItemsHost="True" ScrollViewer.VerticalScrollBarVisibility="Disabled" />
                                                </ItemsPanelTemplate>
                                                <Style x:Key="DataGridColumnHeadersPresenterStyle1" TargetType="{x:Type DataGridColumnHeadersPresenter}">
                                                    <Setter Property="Template">
                                                        <Setter.Value>
                                                            <ControlTemplate TargetType="{x:Type DataGridColumnHeadersPresenter}">
                                                                <Grid>
                                                                    <DataGridColumnHeader x:Name="PART_FillerColumnHeader" AutomationProperties.AutomationId="PAPART_FillerColumnHeader" IsHitTestVisible="False" Visibility="Collapsed"/>
                                                                    <ItemsPresenter/>
                                                                </Grid>
                                                            </ControlTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </Style>
                                            </DataGridColumnHeadersPresenter.Resources>
                                        </DataGridColumnHeadersPresenter>
                                    </ScrollViewer>
                                    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" AutomationProperties.AutomationId="PAPART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" Grid.ColumnSpan="2" Grid.Row="1" />

                                    <ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="PAPART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" 
                                                       Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                                                       Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"
                                               ContextMenu="{x:Null}"/>
                                    <Grid Grid.Column="1" Grid.Row="2">
                                        <Grid.ColumnDefinitions>
                                            <!--<ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>-->
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <ScrollBar Grid.Column="0"
                                                   SmallChange="20"
                                                     AutomationProperties.AutomationId="PA" Name="PART_HorizontalScrollBar"
                                                     Orientation="Horizontal"
                                                     Maximum="{TemplateBinding ScrollableWidth}"
                                                     ViewportSize="{TemplateBinding ViewportWidth}"
                                                     Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                                     Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                                                   ContextMenu="{x:Null}"/>
                                        <ScrollBar x:Name="scbCustom" AutomationProperties.AutomationId="scbPACustom"  Grid.Column="0"   Orientation="Horizontal" Tag="{Binding ElementName=scvColHeaders}" Visibility="Visible"
                                                   SmallChange="20"        
                                                   ContextMenu="{x:Null}">
                                            <ScrollBar.Maximum>
                                                <PriorityBinding>
                                                    <Binding RelativeSource="{RelativeSource AncestorType={x:Type ScrollViewer}}"  Path="ScrollableWidth" Converter="{StaticResource ScrollableWidthConverter}"/>
                                                    <Binding ElementName="scvColHeaders" Path="ScrollableWidth"  Mode="OneWay" />
                                                </PriorityBinding>
                                            </ScrollBar.Maximum>

                                            <ScrollBar.Value>
                                                <PriorityBinding>
                                                    <Binding RelativeSource="{RelativeSource TemplatedParent}"  Path="HorizontalOffset" Mode="OneWay" Converter="{StaticResource ScrollableWidthConverter}"/>
                                                    <Binding ElementName="scvColHeaders" Path="HorizontalOffset"  Mode="OneWay" />
                                                </PriorityBinding>
                                            </ScrollBar.Value>

                                            <ScrollBar.ViewportSize>
                                                <PriorityBinding>
                                                    <Binding RelativeSource="{RelativeSource TemplatedParent}"  Path="ViewportWidth" Converter="{StaticResource ScrollableWidthConverter}" />
                                                    <Binding ElementName="scvColHeaders" Path="ViewportWidth"  Mode="OneWay" />
                                                </PriorityBinding>
                                            </ScrollBar.ViewportSize>
                                        </ScrollBar>
                                    </Grid>
                                </Grid>
                            </ControlTemplate>
                        </ScrollViewer.Template>
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>


2)实例化ValueConverter, 类似:
<McsfPAFEContainee_Converters:ScrollableWidthConverter x:Key="ScrollableWidthConverter" />

3)使用此类, 类似:
private DataGridScrollbarHelper _dtgScrollbarHelper;

dtgScrollbarHelper = new DataGridScrollbarHelper(this.grdStudyList);