Window Resize with Mouse drag? - wpf

I have a WPF Application.I want to implement Resize the window(whatever size) using mouse drag with window corners or sides. like notepad or wordpad.Controls are strecthed according to window size.
can any one help on this?

By default, the Window class (<Window> element) comes with style that is 'Resizeable' - that is a thik border which the user can resize by dragging. If you do nothing to the Visual Studio's generated WPF Window item you'll get a resizeable Window: The property that dictates this behavior is ResizeMode. If you want to have a little Grip, then set the followign property: ResizeMode="CanResizeWithGrip"
There are many ways to acheive controls proportional to the embedding element. One common method is to use the element, and define ahd columns width and row heights in terms of '*'. The example below will drow two buttons that are 2/5 of the height each:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="2*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" />
<Button Grid.Row="1" />
</Grid>

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

Putting Canvas Inside Viewbox in Silverlight With Image

All,
I'm trying to make the silverlight app I'm making resize in a reasonable manner. To do this, I thought I would use a dynamic grid. In the center of the grid I need to put an image inside of a canvas because I will dynamically be positioning objects on top of it. Ideally, as the user makes the browser window larger, the center column would be able to resize and grow larger, thereby growing the image.
Here's what I've got:
<Viewbox Grid.Row="0" Grid.Column="1">
<Canvas x:Name="cvsCenterPane">
<Image x:Name="imgFormImage" MouseLeftButtonDown="imgFormImage_MouseLeftButtonDown"
MouseLeftButtonUp="imgFormImage_MouseLeftButtonUp" MouseMove="imgFormImage_MouseMove" />
</Canvas>
</Viewbox>
In the code behind, I then set the image source.
Here's my grid definition:
<Grid x:Name="LayoutRoot" Background="DarkCyan" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
I think there must be some sort of unhandled exception occurring during the construction of the Viewbox because the image does not display at all. What am I doing wrong here? Am I taking the wrong approach?
There is no exception, the canvas just does not have any set dimensions which is a requirement when you want to use a ViewBox.
I for one would not use a canvas, you can stick with the grid since you can place more than one control in a cell and if you need to move the objects around you can use the Margin or a TranslateTransform in the RenderTransform property.
This does not work because you cannot set the x:Name attribute of children in a Viewbox with silverlight. At least according to a few sources:
http://blog.ningzhang.org/2008/11/viewbox-control-in-silverlight-toolkit.html
http://forums.silverlight.net/forums/p/48535/128830.aspx
http://forums.silverlight.net/forums/p/45789/123941.aspx#123941
this seems to be by design. A few workarounds have been suggested, so I will try them.
Edit: HB seems to be correct, there is no exception, the Canvas needed to have dimensions set.

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