C Sharper
C Sharper

Reputation: 8626

Datagrid horizontal scroll automatically changes when keydown event happens

I am developing WPF application in which there is datagridview.

When to navigate records vertically i use down key, it automatically changes the horizontal scroll also.

Datagrid is as follows:

<Custom:DataGrid  x:Name="dataGridResults"   Margin="33.183,73.372,8,45.742" MouseLeftButtonUp="dgdGlobal_MouseLeftButtonUp"
                         Background="#FF222E44" Foreground="#FF97B2CF" Style="{DynamicResource DataGridStyle1}"
                         RowHeaderStyle="{DynamicResource DataGridRowHeaderStyle1}" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}" 
                         RowStyle="{DynamicResource DataGridRowStyleAuto}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}"
                         CellStyle="{DynamicResource DataGridCellStyleGreen}" 
                         VerticalGridLinesBrush="{x:Null}" RowBackground="#FFC71919" HorizontalGridLinesBrush="{x:Null}"
                         KeyUp="dgdGlobal_KeyUp" DataContext="{Binding}" AllowDrop="True" Drop="dataGridResults_Drop"
                         MouseDoubleClick="dataGridResults_MouseDoubleClick" PreviewMouseLeftButtonDown="dataGridResults_PreviewMouseLeftButtonDown"
                         MouseMove="dataGridResults_MouseMove" MouseRightButtonUp="dataGridResults_MouseRightButtonUp" 
                         SelectionChanged="dgdGlobal_SelectionChanged" MouseWheel="dataGridResults_MouseWheel" 
                         AutoGeneratedColumns="dataGridResults_AutoGeneratedColumns" ColumnReordered="dataGridResults_ColumnReordered"
                         FontFamily="Segoe UI" GridLinesVisibility="None" ScrollViewer.CanContentScroll="True"
                         FocusVisualStyle= "{x:Null}"  MouseEnter="dataGridResults_MouseOver" Sorting="dataGridResults_Sorting" PreviewKeyDown="dgdGlobal_KeyDown" KeyDown="dgdGlobal_KeyDown" AreRowDetailsFrozen="True" HeadersVisibility="None" LoadingRow="dataGridResults_LoadingRow" EnableColumnVirtualization="True" IsReadOnly="True" RenderTransformOrigin="0.5,0.5" Grid.Row="1" Height="Auto"  VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" d:IsHidden="True" ScrollViewer.ScrollChanged="dataGridResults_ScrollChanged" >
            <Custom:DataGrid.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Custom:DataGrid.RenderTransform>
            <Custom:DataGrid.BindingGroup>
                <BindingGroup/>
            </Custom:DataGrid.BindingGroup>
        </Custom:DataGrid>

Is there any attribute or piece of code needed to add?

Please help me.

EDIT:

enter image description here

Upvotes: 0

Views: 276

Answers (1)

C Sharper
C Sharper

Reputation: 8626

I refered this answer:

How to autoscroll on WPF datagrid

Just pasted the code below in scp_RequestBringIntoView:

if (mainDataGrid.Items.Count > 0)
        {
            var border = VisualTreeHelper.GetChild(mainDataGrid, 0) as Decorator;
            if (border != null)
            {
                var scroll = border.Child as ScrollViewer;
                if (scroll != null) scroll.ScrollToEnd();
            }
        }

This piece of code helped me a lot.

Upvotes: 1

Related Questions