Force my grid to scale with 4:3 resolution - wpf

I designed my page to be 4:3 scalable and I'd like to have my grid to scale to 4:3 on any screen.
For example, on a 1900x1200, my grid will scale to 1600x1200 and we'll have 150px on each side of my grid. How can i manage to have this behavior ?
I've started with the following lines :
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="1024*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="768*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1" Grid.Column="1" Name="my_grid" />
</Grid>
But I don't see how to manage the 4:3 thing. I'd like to force my grid to stretch until it has the highest 4:3 resolution possible and then let the free space on the sides.
Do you think I can manage to do this only in WPF ?
Thanks

You could use the ViewBox control. Design your XAML as you have done and place it inside it. The ViewBox will scale to fit the content to the available size. It will not resize the content so everything (ratios, width, etc.) will be left intact.
See http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox.aspx

Thanks to Sascha i did :
<Viewbox Name="grid_jeu" Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" >
<MyControl Name="my_grid" />
</Viewbox>
My Control is designed this way :
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="32" MaxHeight="32"/>
<RowDefinition Height="72" MaxHeight="72"/>
<RowDefinition Height="10" MaxHeight="10"/>
<RowDefinition Height="*"/>
<RowDefinition Height="20" MaxHeight="20"/>
<RowDefinition Height="90" MaxHeight="90"/>
<RowDefinition Height="32" MaxHeight="32"/>
</Grid.RowDefinitions>
</Grid>
I'd like to have only one row and one column which scale. As you can see I tried to put "MaxHeight" and "MaxWidth" to avoid my rows to scale but it doesn't work. When I launch my application I have my 309px column which scales to 420.

Related

WPF Ribbon not scaling like other controls

I'm havnig some beginner's problem with WPF. I want to scale up my ribbon, so that it spans the entire window width, the other controls are doing just that, just my ribbon doesn't want to. It sits in the upper left corner with a width of maybe 100 pixels. I can scale it up manually by giving it a width, but it doesn't take its width from the grid column like the other controls do.
What am I missing?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Custom:Ribbon x:Name="mainRibbon" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top"/>
<DockPanel x:Name="mainPanel" Grid.Row="1" LastChildFill="False"/>
<StatusBar x:Name="mainStatusbar" Grid.Row="2"/>
</Grid>

Grid with RowDefinitions based on content

I am trying to get something like an image with adjustable margins done. The image itself is actually a path and resides in a StackPanel, stretching vertically. The StackPanel's width can be adjusted, and I want to keep the image's ratio and the ratio of path size to margin.
<StackPanel>
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.5*"/>
<RowDefinition Height="0.25*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.25*"/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.25*"/>
</Grid.ColumnDefintions>
<Path Grid.Row="1"
Grid.Column="1"
Data="...blabla..."
Fill="#FF0072C6"
Stretch="UniformToFill"/>
</Grid>
</StackPanel>
Now, the problem is, even though the path object gets drawn in the grid, the first and third rows are always of zero height, instead of getting set based on the height of the 2nd row. I know this happens because the Grid resides in a StackPanel, which ignores automatic height settings and doesn't even let the Grid stretch vertically (or horizontally depending on orientation). It does work if I set a fixed height to the grid but that would ruin the aspect ratio of the image when resizing. How can I get this to work?
This is what I have:
This is what I want:
EDIT:
It seems there is confusion concerning my question. So to hopefully clarify, here's the desired result after resizing the StackPanel:
As you can see, the whole grid is supposed to resize like it was an actual image. The Grid should keep its aspect ratio. This is not about the Path inside the Grid.
If i have correctly understood the problem, a simple solution could be fixing sizes and using a viewbox:
<StackPanel>
<!--The other rows-->
<Viewbox>
<Grid Width="100" Height="100">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="50"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="25"/>
</Grid.ColumnDefinitions>
<Path Grid.Row="1" Grid.Column="1" Data="M8.5,13.5L11,16.5L14.5,12L19,18H5M21,19V5C21,3.89 20.1,3 19,3H5A2,2 0 0,0 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19Z" Fill="Blue" Stretch="Uniform" />
</Grid>
</Viewbox>
</StackPanel>

How to build a 3x3 grid with squared center cell in XAML

I am trying to build a 3x3 grid with XAML (for a Windows Phone application) where the center cell should be a square. I have tried the following but it does not work:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="5*" x:Name="centerColumnDefinition" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="{Binding ElementName=centerColumnDefinition, Path=ActualWidth}" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Canvas Grid.Row="1" Grid.Column="1">
...
</Canvas>
</Grid>
Any suggestions for a working solution?
Greetings from Germany,
Tobias
Try this
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition x:Name="centerColumnDefinition"
Width="5*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Rectangle x:Name="CenterRect"
Grid.Row="1"
Grid.Column="1"
Height="{Binding ElementName=CenterRect,
Path=ActualWidth}"
HorizontalAlignment="Stretch" />
</Grid>
It can be tricky to reference column and row definitions because they are not "real" objects in the visual tree. This method avoids the problem by using a separate object in the center cell that can be used to get the proper sizes.
You should be able to replace the rectangle with another type of control if you want, or just leave it and embed your content inside the rectangle.
ActualHeight and ActualWidth are not set until the control is measured and arranged. Usually there is nothing in InitializeComponent() that causes a measure, so you will need to set the Height of your rows after it's calculated. You can do the re-sizing on Loaded event.
Source: https://stackoverflow.com/a/1695518/546896

WPF: Rectangle in Grid ignores margin

In a user control, which I'm embedding in my main view,
I've defined the following layout:
<Grid x:Name="RootGrid" Margin="0,10,0,0" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="30*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Rectangle Margin="5,0,5,0"
Grid.RowSpan="5"
Grid.ColumnSpan="4"
Panel.ZIndex="-1"
Stroke="Blue"
Fill ="Black"
StrokeThickness="2"
/>
In the preview in Visual Studio, it looks like expected:
- that is the margin 5(for right adjustment) is taken into account.
Unfortunately, during runtime it is another story. I can set the right adjustment (margin) as high as I want, the right border of the rectangle is missing.
Can somebody tell me, what I am doing wrong here? I do not want to work with absolute width for the rectangle (that's working).
Update:
According to the proposal of Erno, I've used a border (and this is indeed much simpler):
<Border HorizontalAlignment="Stretch" Margin="10,0,10,0" Style="{StaticResource StatusPanelBorder}">
<Grid x:Name="RootGrid" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="30*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
But the issue is still the same.
I'm embedding this view in a main view, which has the following layout:
<Grid Width="1600" Height="Auto" HorizontalAlignment="Stretch" Background="{StaticResource NoiseBackground}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30*" />
<ColumnDefinition Width="90*" />
<ColumnDefinition Width="40*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="600" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
The subview is embedded into the last column of the grid.
If I do not use the 'Stretch' alignment it works, but I want to 'stretch' the UI elements.
Second Update:
The issue was just that the shell view had a smaller width. Problem solved!
The best way to add a border to a grid is to take a border and nest a grid inside of it:
<Border>
<Grid>
...
</Grid>
</Border>
This way the grid and the border will resize the way you probably want, you can control the margins and you do not have to keep the textblocks' margins in sync with the rectangle's border.
EDIT
Looking at the xaml you added to the question I guess you set the width of the window to 1600. If so, the width of the Grid that you also set to 1600 doesn't fit because the width of the window INCLUDES the left and right borders. So forcing the grid's width to 1600 will cut it off at the right.
My advice: do not use hard-coded sizes, use star-sizes for columns and rows and use maximized for windows; Grids will stretch their contents automatically.

Border background image will not span entire border

I am writing a WPF application. In the application I have a grid that has two columns. The one column has a border tag that, when the application runs, spans the height of the main window with no issues.
The problem that I am running into is that I want a background image to also span the height of the application and be contained within the border. When I run the application, however, it only takes up enough space to provide background for the controls that exist on the page. This means that more than half of that grid column remains white. I have tried stretching the image (set the stretch to fill) and I know that the image is large enough.
Please, how can I achieve what I am looking for?
Here is the important XAML:
<Border>
<Border.Background>
<ImageBrush ImageSource="../Assets/control bg.png" Stretch="Fill" />
</Border.Background>
<Grid Margin="10,10,10,0">
<Grid.Background>
<ImageBrush />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border BorderBrush="Black" BorderThickness="1">
<TextBlock TextWrapping="Wrap" Text="Owner Information" Margin="5,0,0,0" FontSize="21.333" Foreground="#FF2B2B2B" FontFamily="Verdana"/>
</Border>
<Grid Margin="10,10,5,5" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
The grid does contain some controls which I have left out. They are just textboxes and combos.
Snowbear is right your XAML would be very helpful but here are some things to start off with:
By default, rows and columns in Grids take up the least amount of space necessary to accommodate the largest content within any cell contained in a given row or column. For example, if a column has one cell with a long word like "hippopotamus" contained within it but all the other cells in the column have smaller words like "dog", the width of the column will be the width of the largest word (hippopotamus). "http://msdn.microsoft.com/en-us/library/system.windows.controls.grid.aspx"
So, if your border doesn't have an explicit height and width assigned to it then it will exist just around the content you provided in the column. Binding to a set value would work as well.
<Grid Height="50" Width="50">

Resources