-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2845 from telerik/new-kb-kb-gridview-scroll-to-se…
…lection-925ba16afa314baea682fb05299cd295 Added new kb article kb-gridview-scroll-to-selection
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
title: Programmatically Binding Selected Item Into View in RadGridView | ||
description: Bring the RadGridView selected item into view programmatically | ||
type: how-to | ||
page_title: Scroll Automatically to the Selected Row in RadGridView | ||
slug: kb-gridview-scroll-to-selection | ||
tags: scroll,bring,into,view,selection | ||
res_type: kb | ||
--- | ||
|
||
## Environment | ||
|
||
| Property | Value | | ||
| --- | --- | | ||
| Product | Progress® Telerik® UI for WPF | | ||
| Version | 2024.1.312 | | ||
|
||
## Description | ||
|
||
How to automatically scroll to the selected row in RadGridView for WPF. | ||
|
||
## Solution | ||
|
||
To do that, you can use the `SelectionChanged` event of `RadGridView`. In the event handler, you can access the `GridViewScrollViewer` control and call its `ScrollToVerticalOffset`. The offset can be calculated based on the index of the selected row multiplied by the `RowHeight` setting of the data grid. | ||
|
||
This approach can be wrapped in an attached property that can be re-used in multiple `RadGridView` instances. | ||
|
||
#### __[C#]__ | ||
{{region kb-gridview-scroll-to-selection-0}} | ||
public class GridViewUtilities | ||
{ | ||
public static readonly DependencyProperty ScrollSelectedIntoViewProperty = | ||
DependencyProperty.RegisterAttached("ScrollSelectedIntoView", typeof(bool), typeof(GridViewUtilities), new FrameworkPropertyMetadata(false, ScrollSelectedIntoViewPropertyChanged)); | ||
|
||
public static bool GetScrollSelectedIntoView(DependencyObject obj) | ||
{ | ||
return (bool)obj.GetValue(ScrollSelectedIntoViewProperty); | ||
} | ||
|
||
public static void SetScrollSelectedIntoView(DependencyObject obj, bool value) | ||
{ | ||
obj.SetValue(ScrollSelectedIntoViewProperty, value); | ||
} | ||
|
||
private static void ScrollSelectedIntoViewPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
var gridView = (RadGridView)d; | ||
if ((bool)e.NewValue) | ||
{ | ||
gridView.SelectionChanged += RadGridView_SelectionChanged; | ||
} | ||
else | ||
{ | ||
gridView.SelectionChanged -= RadGridView_SelectionChanged; | ||
} | ||
} | ||
|
||
private static void RadGridView_SelectionChanged(object sender, SelectionChangeEventArgs e) | ||
{ | ||
var gridView = (RadGridView)sender; | ||
if (gridView.SelectedItem != null) | ||
{ | ||
var scrollOffset = gridView.Items.IndexOf(gridView.SelectedItem) * gridView.RowHeight; | ||
var scrollViewer = gridView.ChildrenOfType<GridViewScrollViewer>().First(); | ||
var svo = scrollViewer.VerticalOffset; | ||
var svh = scrollViewer.ViewportHeight; | ||
|
||
if(scrollOffset < svo || scrollOffset > (svo + svh)) | ||
{ | ||
scrollViewer.ScrollToVerticalOffset(scrollOffset); | ||
} | ||
else | ||
{ | ||
scrollViewer.ScrollToVerticalOffset(svo); | ||
} | ||
} | ||
} | ||
} | ||
{{endregion}} | ||
|
||
#### __[XAML]__ | ||
{{region kb-gridview-scroll-to-selection-1}} | ||
<telerik:RadGridView local:GridViewUtilities.ScrollSelectedIntoView="True"/> | ||
{{endregion}} |