0

I'm wondering is it possible to paint this part above scroller, in wpf datagrid, here is image of that I want to say:

enter image description here

So I'm wondering how could I paint that piece of white part exactly above the Scroller, and that blue part on the left is part where my header column is.

Thanks, Cheers

mm8
  • 163,881
  • 10
  • 57
  • 88
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
  • You would probably want to start at looking at how it's Data Template is made. You would need Blend, which generally comes with Visual Studio. So far in WPF, I have been amazed at pretty much everything you can customize. http://stackoverflow.com/questions/8825030/how-to-extract-default-control-template-in-visual-studio – djangojazz Mar 14 '17 at 16:06
  • See [this](http://stackoverflow.com/q/21564631/1997232). Maybe extending vertical scrollbar is better idea? – Sinatr Mar 14 '17 at 16:06

1 Answers1

1

You could modify the control template of the DataGrid and add a white Grid or something to the second column of the first row of the root Grid:

<Style x:Key="DataGridStyle1" TargetType="{x:Type DataGrid}">
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="BorderBrush" Value="#FF688CAF"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
    <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGrid}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                    <ScrollViewer x:Name="DG_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="ApplicationCommands.SelectAll" Focusable="False" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}">
                                        <Button.Visibility>
                                            <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
                                                <Binding.ConverterParameter>
                                                    <DataGridHeadersVisibility>All</DataGridHeadersVisibility>
                                                </Binding.ConverterParameter>
                                            </Binding>
                                        </Button.Visibility>
                                    </Button>
                                    <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1">
                                        <DataGridColumnHeadersPresenter.Visibility>
                                            <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
                                                <Binding.ConverterParameter>
                                                    <DataGridHeadersVisibility>Column</DataGridHeadersVisibility>
                                                </Binding.ConverterParameter>
                                            </Binding>
                                        </DataGridColumnHeadersPresenter.Visibility>
                                    </DataGridColumnHeadersPresenter>
                                    <Grid Background="White" Grid.Column="2" /> <!-- HERE IS THE GRID -->
                                    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" Grid.ColumnSpan="2" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Grid.Row="1"/>
                                    <ScrollBar x:Name="PART_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}"/>
                                    <Grid Grid.Column="1" Grid.Row="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                    </Grid>
                                </Grid>
                            </ControlTemplate>
                        </ScrollViewer.Template>
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsGrouping" Value="True"/>
                <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="False"/>
            </MultiTrigger.Conditions>
            <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
        </MultiTrigger>
    </Style.Triggers>
</Style>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Could I simply remove first line, insted of this – Roxy'Pro Mar 14 '17 at 21:13
  • Yes, that would make it an implicit style that will get applied to all DataGrids in scope that doesn't explicitly set the Style property to some other style. – mm8 Mar 14 '17 at 21:14
  • and what is purpose of this line: because it is causing me an error : "Cannot find the Style Property 'IsVirtualizingWhenGrouping' on the type 'System.Windows.Controls.VirtualizingPanel'. " – Roxy'Pro Mar 14 '17 at 21:17
  • It's included in the default template in PresentationFramework.Classic.dll. You could copy the default template by right-clicking on the DataGrid in design mode in Visual Studio and choose Edit Template->Edit a copy and then add the Grid yourself to this template. – mm8 Mar 14 '17 at 21:25
  • Which version of the .NET Framework are you on? If this property isn't found you might as well remove this condition. – mm8 Mar 14 '17 at 22:14
  • Windows 7 .NET 4.0 – Roxy'Pro Mar 15 '17 at 13:10
  • Then the IsVirtualizingWhenGrouping is not available: http://www.jonathanantoine.com/2011/10/07/wpf-4-5-%E2%80%93-part-11-new-features-for-the-virtualizingpanel/ – mm8 Mar 15 '17 at 15:49