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!