I have some kind of fundamental misunderstanding of how I should approach this problem.
I have a Canvas inside a ScrollViewer. I would like to be able to scale and zoom in on this canvas and configure ScrollViewer accordingly.
Here is the code:
XAML:
<Window x:Class="scrollerProblem.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <ScrollViewer Name="scroller" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <Canvas Background="AntiqueWhite" Name="content" MouseLeftButtonDown="content_MouseLeftButtonDown" MouseRightButtonDown="content_MouseRightButtonDown"> <Rectangle Width="100" Height="100" Canvas.Top="50" Canvas.Left="50" Fill="PaleGoldenrod"></Rectangle> </Canvas> </ScrollViewer> </Window>
And codebehind:
using System.Windows; using System.Windows.Input; using System.Windows.Media; namespace scrollerProblem { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { float Zoom = 1; public Window1() { InitializeComponent(); content.Width = 700; content.Height = 700; } private void content_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { content.Width *= 2; // BLAH content.Height *= 2; // BLAH Zoom *= 2; TransformGroup gridTransforms = new TransformGroup(); gridTransforms.Children.Add(new ScaleTransform(Zoom, Zoom)); gridTransforms.Children.Add(new TranslateTransform(0, 0)); content.RenderTransform = gridTransforms; } private void content_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { content.Width /= 2; // BLAH content.Height /= 2; // BLAH Zoom /= 2; TransformGroup gridTransforms = new TransformGroup(); gridTransforms.Children.Add(new ScaleTransform(Zoom, Zoom)); gridTransforms.Children.Add(new TranslateTransform(0, 0)); content.RenderTransform = gridTransforms; } } }
Unless I specify the lines marked with βBLAHβ, the scrollbars do not change at all ... which is not surprising, since nothing actually resizes the contents of the Canvas. However, if I add BLAH lines, the canvas shrinks / grows, but it also scales at the same time, which means that it does not look right compared to its contents.
I guess I am taking a fundamentally wrong approach, but I donβt understand how to solve it. Is this the right way, with a minor problem, or am I taking a completely wrong tact here?
wpf-controls
Beska
source share