是否可以为ScrollViewer实现“幻灯片滚动”行为?

时间:2010-12-15 09:49:02

标签: .net wpf

对于支持触摸屏的应用程序,我想在我的WPF 3.5应用程序中修改标准的ScrollViewer控件:我希望用户通过单击控件区域并拖动内容来滚动(类似于Apple的iPhone上的行为)。我想通过将样式应用于ScrollViewer来实现这一点,因为(a)我可以自动将样式应用于窗口中的任何ScrollViewer(甚至包含在像ListView这样的控件中)和(b)我可以使用标准Windows如果应用程序在带有键盘和鼠标的普通桌面上运行,则为滚动条样式。

我的第一个想法是为ScrollViewer使用自定义控件模板。默认控件模板为described in the MSDN并使用ScrollContentPresenter。通常,我只是从ScrollContentPresenter派生一个类并在那里添加幻灯片到滚动行为,但由于某种原因,ScrollContentPresenter被密封 - 我无法从中获取。我已经尝试将ScrollContentPresenter放入UserControl,并将用户控件放入ScrollViewer控件模板,但这也不起作用。

是否有一种简单的方法来获取此行为,而无需重新编写所有ScollViewer / ScrollContentPresenter功能?

1 个答案:

答案 0 :(得分:1)

<强>更新
使ScrollBars隐藏并添加MouseLeave以释放滚动

内部带有矩形的幻灯片ScrollViewer

<Grid Width="500"
      Height="250">
    <ScrollViewer Name="slideScrollViewer"
                  Style="{StaticResource slideScrollViewer}"
                  HorizontalScrollBarVisibility="Auto"
                  VerticalScrollBarVisibility="Auto">
        <Rectangle Width="1000"
                   Height="500">
            <Rectangle.Fill>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                    <GradientStop Offset="0.0" Color="Red"/>
                    <GradientStop Offset="0.33" Color="Green"/>
                    <GradientStop Offset="0.66" Color="Blue"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
    </ScrollViewer>
</Grid>

SlidingScrollViewer.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    x:Class="StyleScrollViewer.SlidingScrollViewer">
    <Style x:Key="slideScrollViewer" TargetType="ScrollViewer">
        <Setter Property="HorizontalScrollBarVisibility" Value="Hidden"/>
        <Setter Property="VerticalScrollBarVisibility" Value="Hidden"/>
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="slideScrollViewer_PreviewMouseLeftButtonDown"/>
        <EventSetter Event="PreviewMouseLeftButtonUp" Handler="slideScrollViewer_PreviewMouseLeftButtonUp"/>
        <EventSetter Event="MouseMove" Handler="slideScrollViewer_MouseMove"/>
        <EventSetter Event="MouseLeave" Handler="slideScrollViewer_MouseLeave"/>
    </Style>
</ResourceDictionary>

SlidingScrollViewer.xaml.cs

public partial class SlidingScrollViewer
{
    private bool m_isCaptured = false;
    Point m_lastCoordinate = new Point(-1, -1);
    private void slideScrollViewer_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        m_isCaptured = true;
    }
    private void slideScrollViewer_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        m_isCaptured = false;
        m_lastCoordinate.X = -1;
    }
    private void slideScrollViewer_MouseMove(object sender, MouseEventArgs e)
    {
        if (m_isCaptured == true)
        {
            ScrollViewer scrollViewer = sender as ScrollViewer;
            Point coordinate = e.GetPosition(scrollViewer);
            if (m_lastCoordinate.X >= 0)
            {
                scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + (m_lastCoordinate.X - coordinate.X));
                scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + (m_lastCoordinate.Y - coordinate.Y));
            }
            m_lastCoordinate = coordinate;
        }
    }
    private void slideScrollViewer_MouseLeave(object sender, MouseEventArgs e)
    {
        m_isCaptured = false;
        m_lastCoordinate.X = -1;
    }
}