从Datagrid中的ScrollViewer访问HorizontalOffset [英] Access HorizontalOffset from ScrollViewer in Datagrid
问题描述
< data:DataGrid x:Name =MyGrid
ItemsSource ={Binding Elements}
ScrollViewer.HorizontalScrollBarVisibility =Visible>
但是如何访问ScrollViewers的HorizontalOffset属性?如果我可以绑定在XAML它将是很好的,但它可以是足够的访问它从代码背后。
我试图找到它通过迭代视觉树从DataGrid的代码背后,但我无法掌握它(滚动条总是为空)。我已经尝试了几种不同的FindVisualChild()实现。
var scroller = MyGrid.FindVisualChild< ScrollViewer>();
这可能与事实有关 DataGrid
不使用 ScrollViewer
默认模板管理自己的滚动,并包括两个滚动条。因此,您需要访问水平滚动条。
我使用我自己的 VisualTreeEnumeration
我会在这里找到 。
var scrollBar = MyGrid.Descendents()
.OfType&ScrollBar>()
.FirstOrDefault(sb => sb .Name ==HorizontalScrollbar);
I have a DataGrid from the Silverligt 4.0 SDK. I want to access "HorizontalOffset" from the internal ScrollViewer of the DataGrid. I can access attached properties for it, such as:
<data:DataGrid x:Name="MyGrid"
ItemsSource="{Binding Elements}"
ScrollViewer.HorizontalScrollBarVisibility="Visible">
But how can I access the ScrollViewers HorizontalOffset property? If I could bind it in XAML it would be nice but it could be enough to access it from code behind.
I've tried to find it by iterating the visual tree from the DataGrid in code behind but I can't get hold of it (scroller is always null). I've tried several different implementations of FindVisualChild().
var scroller = MyGrid.FindVisualChild<ScrollViewer>();
It might have something to do with the fact the the DataGrid
doesn't use a ScrollViewer
The default template manages its own scrolling and includes the two scroll bars. You therefore need to access the horizontal scroll bar.
I use my own VisualTreeEnumeration
class the code for which you'll find here.
var scrollBar = MyGrid.Descendents()
.OfType<ScrollBar>()
.FirstOrDefault(sb => sb.Name == "HorizontalScrollbar");
这篇关于从Datagrid中的ScrollViewer访问HorizontalOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!