zoukankan      html  css  js  c++  java
  • ScrollViewer 自动滚动(wpf和Silverlight都可用)

    Xaml文件

    <ScrollViewer x:Name="AutoScrollViewer" KeyUp="AutoScrollViewer_KeyUp">
    </ScrollViewer>

    c#代码 

    使用方法

        //Scroll page vertically when focus changes to a control that is outside the viewable area.
    private void AutoScrollViewer_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Tab)
        {
            FrameworkElement element = FocusManager.GetFocusedElement() as FrameworkElement;
     
            if (element != null)
            {
                ScrollViewer scrollViewer = sender as ScrollViewer;
                try
                {
                    scrollViewer.ScrollToVerticalOffset(GetVerticalOffset(element, scrollViewer));
                }
                catch
                {
                    //if there's an error, don't scroll.
                }
            }
        }
    }
     

    xaml文件

       <ScrollViewer x:Name="scrollViewer" Grid.Column="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Margin="0,5,0,5" MaxHeight="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type Border}}}" DockPanel.Dock="Right">
                    <ListBox x:Name="lbText" Width="220" l:SelectorExtenders.IsAutoscroll="True"   Height="Auto"  SelectionMode="Single"  Margin="0,0,5,0"  RequestBringIntoView="lbText_RequestBringIntoView" SelectionChanged="lbText_SelectionChanged">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel x:Name="spText" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp" Margin="0,6,5,2" Cursor="Hand">
                                <TextBlock x:Name="tbEn" TextWrapping="Wrap" Text="{Binding En}"  Width="200" Margin="0,0,5,0"/>
                                <TextBlock x:Name="tbCh" TextWrapping="Wrap" Text="{Binding Ch}"  Width="200" Margin="0,0,5,0"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                  
                </ScrollViewer>

    在listbox中使用例子

    ListBoxItem myListBoxItem =
     (ListBoxItem)(lbText.ItemContainerGenerator.ContainerFromItem(lbText.Items[i]));
     
                     FrameworkElement element = myListBoxItem as FrameworkElement;
                     scrollViewer.ScrollToVerticalOffset(GetVerticalOffset(element, scrollViewer));
     

    公用function方法:

    private double GetVerticalOffset(FrameworkElement child, ScrollViewer scrollViewer)
    {
        // Ensure the control is scrolled into view in the ScrollViewer. 
        GeneralTransform focusedVisualTransform = child.TransformToVisual(scrollViewer);
        Point topLeft = focusedVisualTransform.Transform(new Point(child.Margin.Left, child.Margin.Top));
        Rect rectangle = new Rect(topLeft, child.RenderSize);
        //If the control is taller than the viewport, don't scroll down further than the top of the control. 
        double controlRectangleBottom = rectangle.Bottom - scrollViewer.ViewportHeight > scrollViewer.ViewportHeight ? scrollViewer.ViewportHeight : rectangle.Bottom;
        double newOffset = scrollViewer.VerticalOffset + (controlRectangleBottom - scrollViewer.ViewportHeight);
        return newOffset < 0 ? 0 : newOffset; // no use returning negative offset
    }
     
    在wpf和Silverlight中都可以用
  • 相关阅读:
    20145310《信息安全系统设计基础》第二周学习总结
    20145310《信息安全系统设计基础》第一周学习总结
    20145310 第0周《信息安全系统设计基础》学习总结
    课程总结
    20145310《Java程序设计》第5次实验报告
    20145310 《Java程序设计》第10周学习总结
    20145310 《Java程序设计》第9周学习总结
    20145310《Java程序设计》第4次实验报告
    20145310《Java程序设计》第3次实验报告
    git的安装以及遇到的问题
  • 原文地址:https://www.cnblogs.com/z_lb/p/2104223.html
Copyright © 2011-2022 走看看