从Datagrid中的ScrollViewer访问Horizo​​ntalOffset [英] Access HorizontalOffset from ScrollViewer in Datagrid

查看:129
本文介绍了从Datagrid中的ScrollViewer访问Horizo​​ntalOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自Silverligt 4.0 SDK的DataGrid。我想从DataGrid的内部ScrollViewer访问Horizo​​ntalOffset。我可以访问附加的属性,例如:

 < data:DataGrid x:Name =MyGrid
ItemsSource ={Binding Elements}
ScrollViewer.Horizo​​ntalScrollBarVisibility =Visible>

但是如何访问ScrollViewers的Horizo​​ntalOffset属性?如果我可以绑定在XAML它将是很好的,但它可以是足够的访问它从代码背后。



我试图找到它通过迭代视觉树从DataGrid的代码背后,但我无法掌握它(滚动条总是为空)。我已经尝试了几种不同的FindVisualChild()实现。

  var scroller = MyGrid.FindVisualChild< ScrollViewer>(); 


解决方案

这可能与事实有关 DataGrid 不使用 ScrollViewer



默认模板管理自己的滚动,并包括两个滚动条。因此,您需要访问水平滚动条。



我使用我自己的 VisualTreeEnumeration 我会在这里找到

  var scrollBar = MyGrid.Descendents()
.OfType&ScrollBar>()
.FirstOrDefault(sb => sb .Name ==Horizo​​ntalScrollbar);


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访问Horizo​​ntalOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆