WPF Programatically scrolling a canvas in a items

2019-07-10 02:56发布

问题:

I have an ItemsControl that contains a canvas within a ScrollViewer. The canvas is large and only a portion of it displays at a time. I want to programatically scroll it (the user clicks and drags the canvas to scroll). I looked through the ScrollViewer methods and tried the following in the mouse event handlers:

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

However, this does nothing. I checked the values of deltaX and deltaY and they are valid values (like 3, 5 etc.). The HorizontalOffset and VerticalOffset remain 0 at all times, even after executing the above lines.

Here is my 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>

Any help/suggestions is appreciated!

回答1:

It works fine (ScrollViewer scrolls) in my test application:

<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>

and the code behind of:

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);
    }

Could you post some more details of the problem you are experiencing?



回答2:

Another potential solution...used for a TreeView, however don't see why the code should not work in your case:

Programmatically scrolling a TreeView