tags:

views:

87

answers:

2

So I'm pretty new to WPF and I'm having trouble with the layout of my Window. Currently I have a WPF application with a Grid defined as such:

    <Grid.RowDefinitions>
        <RowDefinition Height="23" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

In the very top row, spanning two columns, I have a Menu. In the second row I have some Labels in the first column and an Image and a WindowsFormsHost in the second, finally, in the third row I have a graphing control in the second column. By default, I set the size of the Image and WFH to 570 x 413. Now, I'd like for both the Image and WFH to expand when my Window is resized such that they maintain the same relative (to the Window) size. (I actually would like the same to happen to my graphing control as well, but I can probably figure that one out if I can get the others. I cannot for the life of me figure out what setting I need to turn on/off or what I might need to bind or whatever in order to make this happen. Any help is greatly appreciated!

Thanks!

+1  A: 

Have you tried:

<RowDefinition Height="*" />

Make sure to assign your Grid.Row within your image or control, but do not sign the controls height/width properties. The control should autosize with expansion.

Update

Did a quick test to make sure this works.

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="23"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Label Grid.Row="0" Content="Example"/>
        <Image Grid.Row="1" Source="c:\Example.jpg"/>
        <Label Grid.Row="2" Content="Example2"/>
    </Grid>

The image expands with the application based on the image's proportions. It does expand, but it keeps it's dimensions along with the full image in view. If you were to change the rowdefinition from * to Auto, you will notice that the image will expand, but it will expand past your view. Meaning you will not always see the full image.

I would suggest making a simple application like above, and playing with constraints to figure out what each does.

jsmith
Implementing it this way still isn't working for me. My Image IS inside of a Canvas; would that make a difference? I still am not setting the Width or Height properties on that Canvas. I presumed it should have expanded in the same way (and, in the meanwhile, expanded the image equally), but...nothing. I just get a lot more white space on either side.UPDATE: Thanks! I finally just got it working (mostly, my image within canvas still seems off). For some reason, my Grid definition was <Grid Width="847" Height="795"> which screwed it all up! Thanks again, particularly for the code example.
JToland
Be careful using a Canvas - things don't automatically resize on that. Also, you are using a lot of fixed sizes. If you want things to resize automatically, you will probably want to avoid using fixed sizes, and unless you specifically need to use a Canvas, I would suggest using a Grid or other panel. See my answer for something that sizes things automatically - it will make development and maintenance a lot easier for you...
Wonko the Sane
+1  A: 

You need to show more information in your description, because all of the properties of the Grid and of the Image, etc. will factor into the layout.

However, you probably want to look at the HorizontalAlignment and VerticalAlignment properties of your Grid and of your Image, as well as the Stretch property of the Image. Also, you don't want to specify a fixed size for the image (you can specify a MinWidth and a MinHeight if you want them to be a certain size when starting up).

Here's a quick example that shows a grid filling a window, with a scaling image.

<Grid HorizontalAlignment="Stretch"
      VerticalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="23" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

    <Label Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Content="First Row" />
    <Label Grid.Column="0" Grid.Row="1" Content="Column 0, Row 1" />

    <Image Grid.Column="1" Grid.Row="1" 
           HorizontalAlignment="Stretch"
           VerticalAlignment="Stretch"
           Source="Resources\ExamplePicture.png"
           Stretch="Uniform" />

Wonko the Sane