Search code examples
c#-4.0mvvmdatagridwpfdatagrid

How to set scrolling Number of lines at a time by C# in wpf DataGrid?


May someone please tell me that how one can set the scroll number of lines one at a time by c# code behind or by any property in WPF DataGrid.

I know we can set that property from control panel as well but i want that thing from program.

Scroll Bar Code( .Xaml)-

<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
        <Setter Property="Stylus.IsFlicksEnabled" Value="false" />
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrush}}" />
        <Setter Property="Background" Value="{DynamicResource ScollBarBackgroundBrush}" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
        <Setter Property="Width" Value="12" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollBar}">
                    <Border BorderBrush="#e7e7e7" BorderThickness="1">
                    <Grid x:Name="GridRoot"
                          Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidth}}"
                          Background="{TemplateBinding Background}">
                        <Grid.RowDefinitions>
                            <RowDefinition MaxHeight="18" />
                            <RowDefinition Height="0.00001*" />
                            <RowDefinition MaxHeight="18" />
                        </Grid.RowDefinitions>
                        <RepeatButton x:Name="DecreaseRepeat"
                                      Command="ScrollBar.LineUpCommand"
                                      Foreground="{StaticResource StandardBrush}"
                                      Style="{DynamicResource ScrollButtons}">
                            <Path x:Name="DecreaseArrow"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  Data="F1 M 3.5,0L 0,7L 7,7L 3.5,0 Z "
                                  Fill="{StaticResource StandardBrush}" />
                        </RepeatButton>
                        <Track x:Name="PART_Track"
                               Grid.Row="1"
                               Focusable="false"
                               IsDirectionReversed="true">
                            <Track.Thumb>
                                <Thumb x:Name="Thumb"
                                       Background="{DynamicResource ButtonDefaultBrush}"
                                       Style="{DynamicResource ScrollThumbs}" />
                            </Track.Thumb>
                            <Track.IncreaseRepeatButton>
                                <RepeatButton x:Name="PageUp"
                                              Command="ScrollBar.PageDownCommand"
                                              Focusable="false"
                                              Opacity="0" />
                            </Track.IncreaseRepeatButton>
                            <Track.DecreaseRepeatButton>
                                <RepeatButton x:Name="PageDown"
                                              Command="ScrollBar.PageUpCommand"
                                              Focusable="false"
                                              Opacity="0" />
                            </Track.DecreaseRepeatButton>
                        </Track>
                        <RepeatButton x:Name="IncreaseRepeat"
                                      Grid.Row="2"
                                      Command="ScrollBar.LineDownCommand" 
                                      Foreground="{DynamicResource StandardBrush}"
                                      Style="{DynamicResource ScrollButtons}">
                            <Path x:Name="IncreaseArrow"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  Data="F1 M 3.5,7L 7,0L 0,0L 3.5,7 Z "
                                  Fill="{StaticResource StandardBrush}" />
                        </RepeatButton>
                    </Grid>
                    </Border>
                    <ControlTemplate.Triggers>

                        <Trigger SourceName="IncreaseRepeat" Property="IsMouseOver" Value="true">
                            <Setter TargetName="IncreaseArrow" Property="Fill" Value="{StaticResource HoverBrush}" />
                        </Trigger>
                        <Trigger SourceName="DecreaseRepeat" Property="IsMouseOver" Value="true">
                            <Setter TargetName="DecreaseArrow" Property="Fill" Value="{StaticResource HoverBrush}" />
                        </Trigger>

                        <Trigger SourceName="Thumb" Property="IsMouseOver" Value="true">
                            <Setter TargetName="Thumb" Property="Background" Value="{StaticResource HoverBrush}" />
                        </Trigger>
                        <Trigger SourceName="Thumb" Property="IsDragging" Value="true">
                            <Setter TargetName="Thumb" Property="Background" Value="{StaticResource StandardBrush}" />
                        </Trigger>

                        <Trigger SourceName="IncreaseRepeat" Property="IsPressed" Value="true">
                            <Setter TargetName="IncreaseArrow" Property="Fill" Value="{StaticResource StandardBrush}" />
                        </Trigger>
                        <Trigger SourceName="DecreaseRepeat" Property="IsPressed" Value="true">
                            <Setter TargetName="DecreaseArrow" Property="Fill" Value="{StaticResource StandardBrush}" />
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="IncreaseArrow" Property="Fill" Value="{DynamicResource LayerChild1Brush}" />
                            <Setter TargetName="DecreaseArrow" Property="Fill" Value="{DynamicResource LayerChild1Brush}" />
                            <Setter TargetName="Thumb" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="Orientation" Value="Horizontal">
                            <Setter TargetName="GridRoot" Property="LayoutTransform">
                                <Setter.Value>
                                    <RotateTransform Angle="-90" />
                                </Setter.Value>
                            </Setter>
                            <Setter TargetName="PART_Track" Property="LayoutTransform">
                                <Setter.Value>
                                    <RotateTransform Angle="-90" />
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Width" Value="Auto" />
                            <Setter Property="Height" Value="12" />
                            <Setter TargetName="Thumb" Property="Tag" Value="Horizontal" />
                            <Setter TargetName="DecreaseRepeat" Property="Command" Value="ScrollBar.LineLeftCommand" />
                            <Setter TargetName="IncreaseRepeat" Property="Command" Value="ScrollBar.LineRightCommand" />
                            <Setter TargetName="PageDown" Property="Command" Value="ScrollBar.PageLeftCommand" />
                            <Setter TargetName="PageUp" Property="Command" Value="ScrollBar.PageRightCommand" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Solution

  • I got the answer. We just need to set CanContentScroll property to False.

    ScrollViewer.CanContentScroll=False