55773

Get datagrid's scrollviewer

Question:

I'm trying to get the datagrid's scrollviewer to be able to set the offset (which has been stored earlier).

I use this function :

public static T GetVisualChild<T>(DependencyObject parent) where T : Visual { T child = default(T); int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++) { Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); child = v as T; if (child == null) { child = GetVisualChild<T>(v); } if (child != null) { break; } } return child; }

And I call it like this :

this.dataGrid.ItemsSource = _myData; ScrollViewer sc = ressource_design.GetVisualChild<ScrollViewer>(this.dataGrid); if (sc != null) sc.ScrollToVerticalOffset(stateDatagrid.ScrollbarOffset);

And it works in many cases, but in some cases the function returns null and I'm not able to get the scrollviewer.

This call is made just after setting the ItemsSource (ObservableCollection of items) and it works well in 90% cases. The datagrid has not been rendered yet.

I've also tried with the function :

public static ScrollViewer GetScrollViewerFromDataGrid(DataGrid dataGrid) { ScrollViewer retour = null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dataGrid) && retour == null; i++) { if (VisualTreeHelper.GetChild(dataGrid, i) is ScrollViewer) { retour = (ScrollViewer)(VisualTreeHelper.GetChild(dataGrid, i)); } } return retour; }

still null.

Why I'm unable to get the datagrid's scrollviewer ?

I've not pasted my datagrid's style since I have datagrids working with it and it is complicated with many dependencies.

I thought it could be related to virtualization but i'm not able to retrieve the scrollviewer of this datagrid :

<DataGrid Style="{StaticResource StyleDataGrid}" HeadersVisibility="None" ItemsSource="{Binding _Data}" Name="dataGrid1" RowDetailsVisibilityMode="Visible" SelectionChanged="dataGrid1_SelectionChanged">

Answer1:

You need to go recursive through the VisualTree elements. Your function only looks at DataGrid layer. If the ScrollViewer isn't there (see picture) you will not find it.

<a href="https://i.stack.imgur.com/CYp9E.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/CYp9E.png" data-original="https://i.stack.imgur.com/CYp9E.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Try the following function:

public static ScrollViewer GetScrollViewer(UIElement element) { if (element == null) return null; ScrollViewer retour = null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element) && retour == null; i++) { if (VisualTreeHelper.GetChild(element, i) is ScrollViewer) { retour = (ScrollViewer) (VisualTreeHelper.GetChild(element, i)); } else { retour = GetScrollViewer(VisualTreeHelper.GetChild(element, i) as UIElement); } } return retour; }

Recommend

  • Determine height of screen in Java
  • trying to split the file download buffer to into separate threads
  • Error on a disk space usage script
  • PowerShell Flatten Directory Structure
  • 64 bit integer comparison in HAProxy acl rule
  • Parse.com to communicate with WSDL
  • Programmatically edit a datagrid cell in wpf
  • how do I access a radiobutton inside a ListBoxItem in windows phone 7
  • DynamoDb “Query key condition not supported”
  • How to Loop Through CheckBoxes in a TabControl using WPF?
  • Location-aware geocoding on Android (just like Google Maps)
  • Initial DataGrid Sorting
  • Bind customObject to DataGrid
  • Is DataGrid an alternative for UWP?
  • How to get instance of the GridControl in LookupEdit (Devexpress)
  • Get-AdComputer -filter parameter not accepting Get-Date output
  • Return list instead of an item Webservice Phone 8.1 UAP
  • Programmatically setting the record pointer in a C# DataGridView
  • How to use a MultiBinding on DataGridTextColumn?
  • Add items to ComboBox at runtime?
  • Binding ContextMenu Tag to Owner
  • How to prompt user that edits have been made upon changing pages or sorting in Kendo Grid
  • Escaping single quotes in JDBC with MySql
  • is it possible to insert a line break in this tooltip?
  • How to populate html table with info from list in django
  • Prevent page break in text block with iText, XMLWorker
  • Shouldn't else be indented in the below code
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Load html files in TinyMce
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • LevelDB C iterator
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • Why do underscore prefixed variables exist?
  • How to push additional view controllers onto NavigationController but keep the TabBar?