今天遇到一个问题,需要把 打开的页面中 ,显示 其中 ListBox 的最后一项,试探着写了下面的代码,结果成功了,但是回头还需要研究一下,先记下来:

 static T FindChildOfType<T>(DependencyObject root) where T : class
{
var queue = new Queue<DependencyObject>();
queue.Enqueue(root);

while (queue.Count > 0)
{
DependencyObject current = queue.Dequeue();
for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
{
var child = VisualTreeHelper.GetChild(current, i);
var typedChild = child as T;
if (typedChild != null)
{
return typedChild;
}
queue.Enqueue(child);
}
}
return null;
}

调用时:

 ScrollViewer scrollViewer = FindChildOfType<ScrollViewer>(messagesListBox);
if (scrollViewer != null)
{
scrollViewer.ScrollToVerticalOffset(100);
}



相关文章:

  • 2021-11-08
  • 2022-12-23
  • 2022-03-06
  • 2021-09-05
  • 2021-06-05
  • 2021-05-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-08
  • 2021-10-06
  • 2021-07-17
相关资源
相似解决方案