.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Canvas and ScrollViewer resize problems
 

Canvas and ScrollViewer resize problems

hello

ive got a canvas that is used to display polylines, and this canvas has a scaletransform that is linked to a slider, consequently the size of the canvas can increase. however when i wrap the canvas around a scrollviewer, even when the size is increased the scrollviewer cannot/wont scroll.

ive did a little reading around and know that i could give the scrollviewer a size (smaller than the canvas) and then the canvas could be scrolled. however when i do this, and i scale the canvas up, the scrollviewer still does not recognise this.
i found some articles on the web talking about how the scrollviewer does not know the size has changed? (is that correct)

is there any way i can get around this? any easy 'workarounds' or features of the new releases that solve my problem?

thanks for any help! :-)
SPWilkinson
thanks for your answer but ive tried both rendertransform and layouttransform but neither work

the code i am trying to use is:

<ScrollViewer HorizontalScrollBarVisibility="Visible">
<Canvas x:Name="testzoom" ClipToBounds="True">
<Canvas.LayoutTransform>
<ScaleTransform CenterX="0" CenterY="0" ScaleX="{Binding Value, Mode=Default, ElementName=slider2}" ScaleY="{Binding Value, Mode=Default, ElementName=slider2}"/>
</Canvas.LayoutTransform>
<Canvas Width="886" Height="333" Opacity="0.5" Style="{StaticResource myCanvasStyle}"/>
<Polyline PreviewMouseMove="graphPosition" PreviewMouseRightButtonDown="colourdown" PreviewMouseLeftButtonDown="mousedownsize" PreviewMouseLeftButtonUp="mouseupsize" Stroke="Black" StrokeThickness="5" Points="{Binding Path=lPointsPoly}"/>
</Canvas>
</ScrollViewer>

thanks again, stu
SPWilkinson
try setting some initial width and height on the canvas
lee d

You'll need to ensure you're setting your Canvas' LayoutTransform in order to inform the parent scrollviewer of the change in it's size. You are likely setting your Canvas' RenderTransform, which will work fine for your content, but occurs on Render, rather than on Layout, which is what you need.

HTH,

Fred Vandervelde

Fritzenhammer
thanks for your answer but ive tried both rendertransform and layouttransform but neither work

the code i am trying to use is:

<ScrollViewer HorizontalScrollBarVisibility="Visible">
<Canvas x:Name="testzoom" ClipToBounds="True">
<Canvas.LayoutTransform>
<ScaleTransform CenterX="0" CenterY="0" ScaleX="{Binding Value, Mode=Default, ElementName=slider2}" ScaleY="{Binding Value, Mode=Default, ElementName=slider2}"/>
</Canvas.LayoutTransform>
<Canvas Width="886" Height="333" Opacity="0.5" Style="{StaticResource myCanvasStyle}"/>
<Polyline PreviewMouseMove="graphPosition" PreviewMouseRightButtonDown="colourdown" PreviewMouseLeftButtonDown="mousedownsize" PreviewMouseLeftButtonUp="mouseupsize" Stroke="Black" StrokeThickness="5" Points="{Binding Path=lPointsPoly}"/>
</Canvas>
</ScrollViewer>

thanks again, stu
SPWilkinson
try setting some initial width and height on the canvas
lee d
workaround might be to give the scrollviewer a kick in the nuts: force a measure.
Ruurd Boeke
thanks alot!

when i set the canvas to have initial values it worked like a treat!

so am i correct in thinking this works by the scrollviewer knowing an initial value of its child (ie canvas) and then being able to see if the value is different when the layouttransform has completed and so it knows whether to show the scroll bars or not?


SPWilkinson

It's also useful to note that you need to limit the scrollviewers size in some way (ie making it the last child in a dockpanel, with lastChildFill =true, etc) to make this work - if your scrollviewer resizes with your canvas (which can be the default behavior in some scenarios) you'll obviously never get scrollbars.

Glad you got things working.

F.

Fritzenhammer

You can use google to search for other answers

Custom Search

More Threads

• TextBox Focus bind to selecteditem
• Custom Control Icon
• How to set Grid splitter position?
• FindResource() not finding resource defined in XAML
• Which control to use ?
• calling a thread.sleep in a WPF program
• Poor performance using ModelVisualBase and Viewport3D
• Style, ControlTemplate, Custom Control
• Binding ListView Problem
• Binding listview to property of custom control