我有一个ScrollViewer,其中包含一个具有多个控件的网格.用户可以通过控件进行标签,但是最终他们会选中一个不在视图中的控件 – 所以他们必须通过滚动来使控件再次可见.
是否有任何方法使ScrollViewer自动滚动,使聚焦的控件始终可见.没有,有什么办法可以使这项工作,不听每个控件上的GotFocus事件,然后滚动ScrollViewer使控件可见?
目前我正在使用Silverlight 2.
解决方法
我使用Silverlight 3测试了这个.我不知道SL2.
这是我的XAML:
<ScrollViewer Height="200" Width="200" KeyUp="ScrollViewer_KeyUp"> <StackPanel> <Button Content="1" Height="20" /> <Button Content="2" Height="20" /> <Button Content="3" Height="20" /> <Button Content="4" Height="20" /> <Button Content="5" Height="20" /> <Button Content="6" Height="20" /> <Button Content="7" Height="20" /> <Button Content="8" Height="20" /> <Button Content="9" Height="20" /> <Button Content="10" Height="20" /> <Button Content="11" Height="20" /> <Button Content="12" Height="20" /> <Button Content="13" Height="20" /> <Button Content="14" Height="20" /> <Button Content="15" Height="20" /> <Button Content="16" Height="20" /> <Button Content="17" Height="20" /> <Button Content="18" Height="20" /> <Button Content="19" Height="20" /> <Button Content="20" Height="20" /> </StackPanel> </ScrollViewer>
这是代码隐藏:
private void ScrollViewer_KeyUp(object sender,KeyEventArgs e) { ScrollViewer scrollViewer = sender as ScrollViewer; FrameworkElement focusedElement = FocusManager.GetFocusedElement() as FrameworkElement; GeneralTransform focusedVisualTransform = focusedElement.TransformToVisual(scrollViewer); Rect rectangle = focusedVisualTransform.TransformBounds(new Rect(new Point(focusedElement.Margin.Left,focusedElement.Margin.Top),focusedElement.RenderSize)); double newOffset = scrollViewer.VerticalOffset + (rectangle.Bottom - scrollViewer.ViewportHeight); scrollViewer.ScrollToVerticalOffset(newOffset); }
我做的是点击按钮#1和标签,直到我到达按钮#20.它为我工作尝试一下,让我知道它对你有用.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。