Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to put a canvas control into a scrollviewer. The canvas control is dynamic, that means i am creating their children in c# code during runtime.
XML
<ScrollViewer Grid.Row="1">
    <Canvas Name="cvHeats" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</ScrollViewer>

My problem is, that the scrollbars are displayed, but they are disabled. How
can i make the scrollviewer work for a canvas with dynamic content?

Thanks in advance,
Manu
Posted
Updated 11-Aug-16 7:35am
v2

Try to give a size to the Canvas. Something like:


XML
<ScrollViewer Grid.Row="1">
    <Canvas Name="cvHeats" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
            Width="1000" Height="1000" />
</ScrollViewer>
 
Share this answer
 
You can set your controls up in xaml as follows:
C#
<scrollviewer scrollviewer.horizontalscrollbarvisibility="Auto">
                 ScrollViewer.VerticalScrollBarVisibility="Auto">
                <canvas x:name="canMain" xmlns:x="#unknown">
                    <image x:name="imgMain" />
                </canvas>
            </scrollviewer>

Then subscribe to the inner controls size changed event. In my case I'm using a image but you can replace it what ever suits your needs.
C#
imgMain.SizeChanged += ImgMain_SizeChanged;

Then in the set the width and height of your canvas to the new sizes of you image.
C#
private void ImgMain_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            canMain.Height = e.NewSize.Height;
            canMain.Width = e.NewSize.Width;
        }
 
Share this answer
 
C#
Consel.write("HEloo");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900