Windows Phone 8 - Filling a stackpanel with a button - wpf

Suppose I make the following screen in my app (this is just an example, actual layout is a bit more complex but works on the same principle):
Vertical StackPanel
TextBlock (fits/wraps its content size AKA Auto size)
Button 1 (Auto)
Button 2
I would like to make Button 2 stretch so that it occupies the remainder of the screen's height, so there cannot be a specific Height property - but VerticalAlignment="Stretch" and VerticalAlignment="Bottom" don't do the trick, and in fact don't do anything at all. Is there any way to handle that?

in this case i generally prefer to use a grid, you can use something like this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
</Grid>

Change the StackPanel to a Grid with 3 rows, first two of Auto size and the last *.

Related

Vertical sizing in WPF trouble

I'm having trouble vertically sizing the CheckListBox control from https://wpftoolkit.codeplex.com/. I'm a week-old newbie in WPF, so this is probably a general issue with (vertical) sizing.
Given my grid, I want the first 5 rows to size whatever size, they need to be, and in my viewport, I want the CheckListBox control in row 6 to simply fill the remaining space vertically.
My problem is, that when I fill the CheckListBox with a lot of elements, the control just expands / overflows the viewport. What I want it to do, is to assume the height of the remaining space vertically, which will make it display an inner scrollbar.
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Row="0">Some title</Label>
<RadioButton Grid.Row="1" GroupName="NameSortRadioButtonGroup">Firstname Lastname</RadioButton>
<RadioButton Grid.Row="2" GroupName="NameSortRadioButtonGroup">Lastname, Firstname</RadioButton>
<TextBox Grid.Row="3" Margin="0,5" />
<Label Grid.Row="4" Content="{Binding ElementName=MyCheckListBox, Path=SelectedItems.Count}" />
<xctk:CheckListBox
Grid.Row="5"
Name="MyCheckListBox"
VerticalAlignment="Stretch"
VerticalContentAlignment="Stretch"
Height="Auto"
/>
</Grid>
I have left out some parent elements to simplify. The Grid is in a grid, that's in a canvas, that's in a grid, and this outer grid is direct child of the Window element.
I have tried wrapping the CheckListBox control in a StackPanel with Height="Auto" (to eliminate the possibility of a bug in the CheckListBox) and also here to set the height of the CheckListBox using
Height="{Binding ElementName=CheckListBoxStackPanelWrapper, Path=ActualHeight}"
I also initially tried to do the whole thing with first a StackPanel and later a DockPanel instead of the Grid control.
In all cases, the CheckListBox just flows out of the window/viewport. If I give it an explicit height, it behaves correctly, but I obviously want my window to be resizable.
What shall I do?
To sum up comments because your outer control is a Canvas which does not restrict its child elements From MSDN:
Child elements of a Canvas are always given the full size that they desire
which means that Grid will be given as much space as it require. One solution is to remove the Canvas part or restrict the size size of Grid or expanding row

MulitGrid with Child Grids Contents being sized accordingly

I dont know if this has been answered but what i am trying to to do is i have a parentGrid with 3 rows, the first two are a menu strip and a toolbar, the third row is a child grid that is populated with the choices that are selected from the tool bar. the problem is that i want the forms that load in the child grid to not have their height surpass what i have set that row equal to in the parentGrid.
is there an effective way to do this?
here is basically what i have for code
<Grid x:Name="ParentGrid" Margin="0" >
<Grid.RowDefinitions>
<RowDefinition Height="22"></RowDefinition>
<RowDefinition Height="90"></RowDefinition>
<RowDefinition Height="544*"></RowDefinition>
<!--516<RowDefinition Height="80*"></RowDefinition>
<RowDefinition Height="10*"></RowDefinition>-->
</Grid.RowDefinitions>
<DockPanel></DockPanel><!--Dock Panel Menu Strip-->
<DockPanel></DockPanel><!-- Dock Panel ToolBar-->
<Grid x:Name="ChildGrid" Grid.Row="2" Height="544">
</Grid>
</Grid>
Set the Height in your RowDefinition to whatever you want your Grid's height to be, then create an extra RowDefinition with a Height="*" to take up the remaining space
<Grid x:Name="ParentGrid" Margin="0" >
<Grid.RowDefinitions>
<RowDefinition Height="22"></RowDefinition>
<RowDefinition Height="90"></RowDefinition>
<RowDefinition Height="544"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<DockPanel></DockPanel><!--Dock Panel Menu Strip-->
<DockPanel></DockPanel><!-- Dock Panel ToolBar-->
<Grid x:Name="ChildGrid" Grid.Row="2">
</Grid>
</Grid>
The * in your RowDefinitions mean "all remaining space". Adding a number to it defines the ratio of remaining space that column will get. For example, if you say one column is going to be 1* and another is going to be 2*, it means the 2nd column is going to take up twice the space as the first one. If you have one column taking up 544* and another taking up 10*, it means the available space will get split into 554 parts, and 544 parts will get given to the first column and 10 parts will be given to 2nd column.
The solution that worked for me was that instead of using a Grid as the inner child i used a ScrollViewer as the container

How to put image on top of fullscreen

I am creating silverlight video player.
I have organized my player as grid.
First row is video, second and third are control panels with buttons.
When I switch to fullscreen mode I want to set specified image on the top.
So i load image, resize and can't make this image to cover all elements(video,panels).
I try to make this image only visible, and cant do that.
I think there is possible to create an "element" and set it somehow to cover.
I would like do this like splash because i guess collapsing my rows isn't good idea.
Thanks in advance.
You should be able to do this by first making sure your image element is the last child in the Grid and by adding Grid.RowSpan value to it containing the number of rows in the grid:-
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<MediaElement ... />
<StackPanel Orientation="Horizontal" Grid.Row="1">...</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="2">...</StackPanel>
<Image Source="SomeImageUrl" Grid.RowSpan="3" />
</Grid>
An alternative to placing the Image at the end of the Grid's child list is to give it a higher ZIndex using rather unintuitively Canvas.ZIndex property.

WPF layout with several fixed height parts and certain parts relative to window size

At moment my main layout consists of vertically oriented stack panel and it looks like this:
Root StackPanel
StackPanel - fixed Height 150
(horizontal orientation)
StackPanel - relative Height must be
behalf of free space left on screen
(but at least 150 px). Used by Telerik
GridView Control, if I don't specify Height or MaxHeight Telerik GridView Height becomes very large and does not fit my window.
StackPanel - fixed Height 100
(horizontal orientation)
StackPanel - relative Height must be
half of free space left on screen
(but at least 150 px). Used by Telerik
GridView Control, if I don't specify Height or MaxHeight Telerik GridView Height becomes very large and does not fit my window.
StackPanel - fixed Height 100
(horizontal orientation)
The view must totally fit available screen size.
The problem is that I don't understand how to make certain areas of my view resize depending on available screen size.
Is there is easy way to solve it, or should I be binding to Window height property and doing math?
Thank You very much!
I dont have the telerik controls to hand to test so this is off the top of my head - can you not use a grid as the basis of your control rather than a stackpanel like this? - if this is still a problem then post some code up and we can take a look at what you are trying to achieve.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="150"/>
<RowDefinition Height="*"/>
<RowDefinition Height="100"/>
<RowDefinition Height="*" />
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" />
<StackPanel Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<StackPanel Grid.Row="2" />
<StackPanel Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<StackPanel Grid.Row="4" />
</Grid>

Wpf control size to content?

How do I make a WPF control to change its size according the content in it?
For most controls, you set its height and width to Auto in the XAML, and it will size to fit its content.
In code, you set the width/height to double.NaN. For details, see FrameworkElement.Width, particularly the "remarks" section.
I had a problem like this whereby I had specified the width of my Window, but had the height set to Auto. The child DockPanel had it's VerticalAlignment set to Top and the Window had it's VerticalContentAlignment set to Top, yet the Window would still be much taller than the contents.
Using Snoop, I discovered that the ContentPresenter within the Window (part of the Window, not something I had put there) has it's VerticalAlignment set to Stretch and can't be changed without retemplating the entire Window!
After a lot of frustration, I discovered the SizeToContent property - you can use this to specify whether you want the Window to size vertically, horizontally or both, according to the size of the contents - everything is sizing nicely now, I just can't believe it took me so long to find that property!
I had a user control which sat on page in a free form way, not constrained by another container, and the contents within the user control would not auto size but expand to the full size of what the user control was handed.
To get the user control to simply size to its content, for height only, I placed it into a grid with on row set to auto size such as this:
<Grid Margin="0,60,10,200">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<controls1:HelpPanel x:Name="HelpInfoPanel"
Visibility="Visible"
Width="570"
HorizontalAlignment="Right"
ItemsSource="{Binding HelpItems}"
Background="#FF313131" />
</Grid>
If you are using the grid or alike component:
In XAML, make sure that the elements in the grid have Grid.Row and Grid.Column defined, and ensure tha they don't have margins. If you used designer mode, or Expression Blend, it could have assigned margins relative to the whole grid instead of to particular cells.
As for cell sizing, I add an extra cell that fills up the rest of the space:
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

Resources