如何将 WPF ScrollViewer 的内容滚动到特定位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6336441/
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 to scroll WPF ScrollViewer's content to specific location
提问by Metro
I am writing my custom WPF ItemsControl to display a list of item. The items are shown embedded inside a ScrollViewer:
我正在编写我的自定义 WPF ItemsControl 来显示项目列表。这些项目显示为嵌入在 ScrollViewer 中:
<Style TargetType="MyCustomItemsControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MyCustomItemsControl">
<ScrollViewer x:Name="PART_MyScrollViewer" >
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I want to make sure that when I move the mouse into the control, a particular item (marked as selected) will be scrolled into the mouse position. In my OnMouseEnter method I am able to find the item but I don't know what to do next. Does anyone have any idea?
我想确保当我将鼠标移动到控件中时,特定项目(标记为选定)将滚动到鼠标位置。在我的 OnMouseEnter 方法中,我能够找到该项目,但我不知道下一步该怎么做。有谁有想法吗?
protected override void OnMouseEnter(MouseEventArgs e)
{
for (int i = 0; i < Items.Count; i++)
{
ContentPresenter uiElement = (ContentPresenter)ItemContainerGenerator.ContainerFromIndex(i);
var item = uiElement.Content as MyCustomObject;
if (item.IsSelected)
{
// How to scroll the uiElement to the mouse position?
break;
}
}
}
回答by epox
// How to scroll the uiElement to the mouse position?
uiElement.BringIntoView();
REF: https://msdn.microsoft.com/en-us/library/ms598110.aspx
参考:https: //msdn.microsoft.com/en-us/library/ms598110.aspx
UPDATE: (thanks to @jmbpiano) Note, it does not bring the control exactly to the current mouse cursor position. It just brings the control to a visible position, where the Operator will be able to click it with the mouse (which in 99% of cases is all someone who finds this question is likely to need).
更新:(感谢@jmbpiano)注意,它不会将控件准确地带到当前鼠标光标位置。它只是将控件带到一个可见位置,操作员将能够在该位置用鼠标单击它(在 99% 的情况下,发现此问题的人可能都需要)。
回答by H.B.
Something like the following:
类似于以下内容:
var sv = (ScrollViewer)Template.FindName("PART_MyScrollViewer", this); // If you do not already have a reference to it somewhere.
var ip = (ItemsPresenter)sv.Content;
var point = item.TranslatePoint(new Point() - (Vector)e.GetPosition(sv), ip);
sv.ScrollToVerticalOffset(point.Y + (item.ActualHeight / 2));
回答by Robert Levy
Use UIElement.TranslatePoint() to calculate what position you want to scroll to
使用 UIElement.TranslatePoint() 计算要滚动到的位置
Use ScrollViewer.ScrollToVerticalOffset() to do the scrolling
使用 ScrollViewer.ScrollToVerticalOffset() 进行滚动
回答by Nagendra Velaga
Try this below code :
试试下面的代码:
private void ScrollViewerFromVSTree(DependencyObject element, double pos)
{
try
{
int totalElementcount = VisualTreeHelper.GetChildrenCount(element);
for (int counter = 0; counter < totalElementcount; counter++)
{
DependencyObject ele = VisualTreeHelper.GetChild(element, counter);
if (ele.GetType().Name == "ScrollViewer")
{
ScrollViewer scrollViewer = ele as ScrollViewer;
if (pos > "YourAssumption") // for me it is 610
{
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + 45);
}
else if (pos < "YourAssumption") //for me it is 40
{
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - 45);
}
break;
}
ScrollViewerFromVSTree(VisualTreeHelper.GetChild(element, counter), pos);
}
}
catch (Exception)
{
}
}