如何访问 Silverlight/C# 中 ListBox 控件的 ScrollViewer 元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/263485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you access the ScrollViewer element of a ListBox control in Silverlight/C#?
提问by Yttrium
I wish to dynamically change the scroll position of a Silverlight ListBox from C#, and I need to know how to access the ScrollViewer element of a ListBox control from C#?
我希望从 C# 动态更改 Silverlight ListBox 的滚动位置,并且我需要知道如何从 C# 访问 ListBox 控件的 ScrollViewer 元素?
Thanks guys, Jeff
谢谢你们,杰夫
回答by Bryant
Good question. I didn't find a way to do it directly, but came fairly close by looking at the Silverlight Controls project (they use the scrollviewer on the items control in some of the classes). Here is how you can get it, but it requires a custom listbox:
好问题。我没有找到直接执行此操作的方法,但是通过查看 Silverlight Controls 项目(他们在某些类中的项目控件上使用滚动查看器),我非常接近。以下是获取它的方法,但它需要一个自定义列表框:
public class TestBox : ListBox
{
private ScrollViewer _scrollHost;
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
var itemsHost = VisualTreeHelper.GetParent(element) as Panel;
for (DependencyObject obj = itemsHost; obj != item && obj != null; obj = VisualTreeHelper.GetParent(obj))
{
ScrollViewer viewer = obj as ScrollViewer;
if (viewer != null)
{
_scrollHost = viewer;
break;
}
}
base.PrepareContainerForItemOverride(element, item);
}
}
There might be another way to hook into that event (or another way to get that panel), If you look at the template for the ListBox you will see the scroll viewer is actually named "ScrollViewer", however the GetTemplateChild method is protected so you would still need to create a custom class.
可能有另一种方法可以连接到该事件(或获取该面板的另一种方法),如果您查看 ListBox 的模板,您将看到滚动查看器实际上被命名为“ScrollViewer”,但是 GetTemplateChild 方法受到保护,因此您仍然需要创建一个自定义类。
回答by Dan
From within a class that inherits from the ListBox class, you can use the Protected GetTemplateChild():
在从 ListBox 类继承的类中,您可以使用 Protected GetTemplateChild():
var myScrollviewer = myListBox.GetTemplateChild("ScrollViewer") as ScrollViewer;
var myScrollviewer = myListBox.GetTemplateChild("ScrollViewer") as ScrollViewer;
If you want to access this from outside the ListBox, then exposing the ScrollViewer via a Property should work, again through inheritance.
如果您想从 ListBox 外部访问它,那么通过属性公开 ScrollViewer 应该可以工作,再次通过继承。
CAVEAT: If you have set your own custom template, then this Scrollviewer may not exist. You can use the templates Scrollviewer name instead of the "ScrollViewer" in the method above.
注意:如果您设置了自己的自定义模板,则此 Scrollviewer 可能不存在。您可以使用模板 Scrollviewer 名称而不是上述方法中的“ScrollViewer”。
回答by Sergii
ScrollViewer scrollViewer = yourListBox.getScrollHost();
Is null if no datasourse set to the listbox, in my case it return properly UI Element only after below code executed
如果没有数据源设置到列表框,则为 null,在我的情况下,它只有在执行以下代码后才正确返回 UI 元素
myListBox.ItemsSource = list;
回答by E.Létondor
You can call :
您可以致电:
myListBox.ApplyTemplate();
to force the ListBox visual tree to be created, otherwise GetTemplateChild() will return Null if you attempt to access it immediatly.
强制创建 ListBox 可视化树,否则如果您尝试立即访问它,GetTemplateChild() 将返回 Null。
This works well combined with "Erno de Weerd" explanation : inherit ListBox to be able to call GetTemplateChild() method.
这与“ Erno de Weerd”解释相结合非常有效:继承 ListBox 以能够调用 GetTemplateChild() 方法。
I also tried :
我也试过:
- to use ListBox extension method "GetScrollHost()" but it never worked for me (even after full page initialisations).
- "FindName()", but it didn't work, even when i specified the ScrollViewer name into the ListBox Template.
- 使用 ListBox 扩展方法“GetScrollHost()”但它从来没有对我有用(即使在整页初始化之后)。
- “FindName()”,但它不起作用,即使我在 ListBox 模板中指定了 ScrollViewer 名称。
Emmanuel (Silverlight 3)
伊曼纽尔(银光3)
回答by Yop
Let's make it easy... In your Listbox template, you might find the ScrollViewer Control. Add a Loaded Method for it, and you will get itself frome the sender arg.
让我们简单点... 在您的列表框模板中,您可能会找到 ScrollViewer 控件。为它添加一个加载方法,你将从发送者 arg 中获取自身。
private void ScrollViewer_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
myScrollViewer = (sender as ScrollViewer);
}
this works for me
这对我有用