none
Canvas does not displayed when inside Viewbox. RRS feed

  • Question

  • Hi. I'm novice in WPF.
    I want to draw a graphic primitives over some non-scalable image.
    So, I place Image control, Viewbox and ScrollViewer. This code has a required behavior, but I don't know how to draw primitive over the Image.


    <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="20" />
            </Grid.RowDefinitions>
            <ScrollViewer Grid.Row="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                 <Viewbox Stretch="None"> 
                     <Image Source="c:\image.bmp" />
                </Viewbox>
            </ScrollViewer>
            <StatusBar Grid.Row="1">
                <StatusBarItem Name="cursorPosX">X:</StatusBarItem>
                <StatusBarItem>
                    <Separator  />
                </StatusBarItem>
                <StatusBarItem Name="cursorPosY">Y:</StatusBarItem>
            </StatusBar>
        </Grid>


    Next, I wrote another XAML by replacing Image control with Canvas and ImageBrush.


    <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="20" />
            </Grid.RowDefinitions>
            <ScrollViewer Grid.Row="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                 <Viewbox Stretch="None"> 
                    <Canvas>
                        <Canvas.Background>
                            <ImageBrush ImageSource="c:\image.bmp" />
                        </Canvas.Background>
                    </Canvas>
                </Viewbox>
            </ScrollViewer>
            <StatusBar Grid.Row="1">
                <StatusBarItem Name="cursorPosX">X:</StatusBarItem>
                <StatusBarItem>
                    <Separator  />
                </StatusBarItem>
                <StatusBarItem Name="cursorPosY">Y:</StatusBarItem>
            </StatusBar>
        </Grid>

    In runtime I can see an empty form without picture. Adding graphical primitives into Canvas (for example Rectangle) has no any effects. But canvas content will be properly displayed if I put Canvas Into ScrollViewer directly.


    • Edited by eLVik Tuesday, December 29, 2009 5:21 PM formatting check
    Tuesday, December 29, 2009 5:20 PM

Answers

All replies