2

我有一个ItemsControl包含一个画布的ScrollViewer. 画布很大,一次只显示一部分。我想以编程方式滚动它(用户单击并拖动画布进行滚动)。我查看了 ScrollViewer 方法并在鼠标事件处理程序中尝试了以下操作:

var scrollViewer = (sender) as ScrollViewer;
scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + deltaX);
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + deltaY);

但是,这无济于事。我检查了 deltaX 和 deltaY 的值,它们是有效值(如 3、5 等)。和始终保持为 0,即使在执行上述行之后也是如此HorizontalOffsetVerticalOffset

这是我的 XAML:

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
              MouseUp="ScrollViewer_MouseUp" MouseMove="ScrollViewer_MouseMove"
              PreviewMouseLeftButtonDown="ScrollViewer_PreviewMouseLeftButtonDown" Background="Transparent">
        <ItemsControl ItemsSource="{Binding BubbleVMCollection}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                   <!-- My template here -->
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="Canvas.Left" Value="{Binding AbsoluteLeft}" />
                    <Setter Property="Canvas.Top" Value="{Binding AbsoluteTop}" />
                </Style>
            </ItemsControl.ItemContainerStyle>
        </ItemsControl>
</ScrollViewer>

任何帮助/建议表示赞赏!

4

2 回答 2

1

它在我的测试应用程序中运行良好(ScrollViewer 滚动):

<ScrollViewer Name="scrollViewer"
        VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
          MouseUp="ScrollViewer_MouseUp" MouseMove="ScrollViewer_MouseMove"
          PreviewMouseLeftButtonDown="ScrollViewer_PreviewMouseLeftButtonDown" Background="Transparent">
        <ItemsControl ItemsSource="{Binding BubbleVMCollection}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas Width="5000" Height="5000"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <!-- My template here -->
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="Canvas.Left" Value="{Binding AbsoluteLeft}" />
                    <Setter Property="Canvas.Top" Value="{Binding AbsoluteTop}" />
                </Style>
            </ItemsControl.ItemContainerStyle>
        </ItemsControl>
    </ScrollViewer>

以及后面的代码:

Point capturePoint { get; set; }

    private void ScrollViewer_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
        scrollViewer.CaptureMouse();
        capturePoint = e.MouseDevice.GetPosition(scrollViewer);
    }

    private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e) {
        scrollViewer.ReleaseMouseCapture();
    }

    private void ScrollViewer_MouseMove(object sender, MouseEventArgs e) {
        if (!scrollViewer.IsMouseCaptured) return;
        Point currentPoint = e.MouseDevice.GetPosition(scrollViewer);
        var deltaX = capturePoint.X - currentPoint.X;
        var deltaY = capturePoint.Y - currentPoint.Y;
        scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + deltaX);
        scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + deltaY);
    }

您能否发布有关您遇到的问题的更多详细信息?

于 2010-10-04T15:59:43.587 回答
0

另一个潜在的解决方案......用于 TreeView,但不明白为什么代码不适合您的情况:

以编程方式滚动 TreeView

于 2010-10-04T14:57:36.877 回答