Search code examples
wpfcanvasautosize

Dynamic size canvas with Scroll bars


I am developing a simple WPF application without any auto layout. The goal is when a user clicks (mouse down) a element (say textBlock) will appear at the location of the mouse click. For this I am using canvas panel embedded in a Grid of 1 row, 1 column and scrollviewer (visible). The issues are: 1. when the application window is resized the scroll viewers do not become active. 2. I want the ability to auto grow the canvas with mouse drag. Something like in MS-Excel when user drags the mouse horizontally/vertically the canvas should grow.

I have searched net a lot to figure this out and am unable to get an answer. Any help in this regard would be great. Thanks a bunch in advance.

-P


Solution

  • I after asking this question I figured it out how to have freeform layout and autosize. Here is a sample XAML if anyone needs it or has better suggestion to improve this:

            <Ellipse Grid.Column="0" Fill="Red"/>
            <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch"/>
    
            <!-- Creating a grid with one row and one column"-->
            <ScrollViewer x:Name="ServerLiistCanvasScrollViewer" 
                        HorizontalScrollBarVisibility="Auto" 
                        VerticalScrollBarVisibility="Auto"                         
                          Height="Auto" Width="Auto"
                        Grid.Column="2" >
    
                <Grid x:Name="drawingGrid" Grid.Column="2" 
                      VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
                      Background="Pink"
                      MouseDown="handleCanvasMouseDown">
                </Grid>        
            </ScrollViewer>
    
    
        </Grid>