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中都可以用

你可能感兴趣的:(silverlight)