How to resize content when maximize window in WPF? - c#

I have this window and I would like to resize content ( fit it to the size of window ) when the user maximize this window , I've tried many solutions but any of them worked for me :
<Window x:Class="Window_Image"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="550" d:DesignWidth="550">
<Grid>
<DockPanel LastChildFill="True" Name="ImgDock" Background="Transparent" >
<Image Source="../res/myimage.png" Width="450" Height="450" x:Name="ImageComp" Visibility="Visible" >
</Image>
</DockPanel>
</Grid>
One of solution that I've tried :
https://stackoverflow.com/a/47990733/19442413
Unfortunatly , this solution didn't works for me.

The issue was that the Image size was hardcoded (Height and Width properties).
By default, most of WPF's component will adjust their size to fill their containers.

Related

WPF window with WindowChrome resizing issue

Have this simple XAML for a custom window. It works fine except during a resize (from the left, top, top-left and bottom-left) it looks awful. The window flashes like crazy. Doesn't seem like an OS issue as Microsoft's glass Windows app resize smooth as butter.
I'm trying to customize the window frame, but unless I use WindowChrome, I lose all the resizing, dragging, etc.
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
WindowStyle="None"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Title="MainWindow" Height="450" Width="600">
<WindowChrome.WindowChrome>
<WindowChrome GlassFrameThickness="1,1,1,1" />
</WindowChrome.WindowChrome>
<Border BorderBrush="Red" BorderThickness="1">
<Button Content="Test" Height="25" Width="75" />
</Border>
</Window>

Grid Container resize with height and width specified - C# WPF Anchoring

When using Margins to specify top/left/bottom/right positions, the location of my button and inner grids is not what I expected with the following xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" x:Class="WpfApplication1.MainWindow"
Title="Dialog" WindowStartupLocation="CenterScreen" ResizeMode="CanResize" Height="399" Width="590">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sys="clr-namespace:System;assembly=mscorlib" x:Name="TopÆLeftÆRight596" Background="#FFFFFF">
<Grid Margin="150, 85, 153, 110" x:Name="Panel597" Background="#EEEEEE">
<Button Margin="97,77,108,101" x:Name="BLANK_BUTTON598" ToolTip="Blank" Cursor="Arrow" FontFamily="Arial" FontSize="12" FontWeight="Normal" Foreground="#000000" Content="Blank" />
</Grid>
</Grid>
I believe what is happening is that because my container grid is set to stretch (x:Name="TopÆLeftÆRight596"), the margins are calculated off the window which includes the title bar. If I set height and width on that grid, resizing of the inner controls is no longer possible.
I need a way to specify the height/width values on the window, or perhaps another type of container, so that the margin calculations do not include the title bar, and the inner contents grow and shrink when the window is resized.
Please keep in mind I have no control over the left/top/right/bottom margin values provided.
In essence, what I'm trying to do is duplicate top/left/bottom/right anchoring of the Grid(x:Name="Panel597") and Button(x:Name="BLANK_BUTTON598") controls.
Any help is greatly appreciate. Thanks.

How to set a resize limit for a window?

Lets say I have a xaml for my window like this:
<Window x:Class="WavePoint_MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FontSize="13"
FontFamily="Verdana"
xmlns:local="clr-namespace:WavePoint_MVVM"
Height="{Binding SystemParameters.PrimaryScreenHeigth}"
Width="{Binding SystemParameters.PrimaryScreenWidth}"
AllowsTransparency="False"
WindowStartupLocation="CenterScreen"
ResizeMode="CanResizeWithGrip"
<Grid/>
</Window>
Now I want to fix some sort of limit in which the user can't resize the window beyond it. Just to give an example, in Spotify app for Windows desktop, the user can't resize beyond a certain limit. This is the minimum I can get:
Thoughts?
To force a minimum window size, set your MinWidth and MinHeight attributes on the Window:
<Window x:Class="WavePoint_MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FontSize="13"
FontFamily="Verdana"
xmlns:local="clr-namespace:WavePoint_MVVM"
Height="{Binding SystemParameters.PrimaryScreenHeigth}"
Width="{Binding SystemParameters.PrimaryScreenWidth}"
AllowsTransparency="False"
WindowStartupLocation="CenterScreen"
ResizeMode="CanResizeWithGrip"
MinWidth="640"
MinHeight="480">
<Grid/>
</Window>

Unable to fill entire space with WPF control in WinForm

I created Wpf UserControl and am hosting it in WinForm.
<UserControl x:Class="Sapphire.WpfUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Height="527" Width="992">
<Canvas x:Name="videoCanvas" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" >
<Label Canvas.ZIndex="2" Content="Label" Canvas.Left="165" Canvas.Top="50" Width="125" Foreground="#FFFFFEFF"/>
<MediaElement x:Name="videoElement" Canvas.ZIndex="1" Canvas.Left="10" Canvas.Top="10" />
</Canvas>
As shown in designer file this WPF control is hosted through HostElement:
//
// elementHost1
//
this.elementHost1.Dock = System.Windows.Forms.DockStyle.Fill;
this.elementHost1.Location = new System.Drawing.Point(0, 0);
this.elementHost1.Name = "elementHost1";
this.elementHost1.Size = new System.Drawing.Size(1130, 593);
this.elementHost1.TabIndex = 2;
this.elementHost1.Text = "elementHost1";
this.elementHost1.Child = this.wpfUserControl1;
So it looks all correct. You also can see that the DockStyle is Fill.
However, the WPF control does not fill the entire WinForm and always shows up of a size as set and displayed in Designer.
I removed the Height and Width both from Canvas and from MediaElement that Canvas contains but it did not have any effect...
I'd appreciate if somebody can point out what I am doing wrong here - I am new to WPF.
You need to remove the Width and Height of the <UserControl> so that the containing ElementHost controls the size of the contained elements:
<UserControl x:Class="Sapphire.WpfUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
If you want a specific size in the designer, you can use d:DesignHeight and d:DesignWidth attributes:
<UserControl x:Class="Sapphire.WpfUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignHeight="527" d:DesignWidth="992">

Hide the default resizegrip in wpf

Is it possible to hide the default resize grip in wpf, I have a custom window without handles on the sides and are currently using:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="100" Width="200" ResizeMode="CanResizeWithGrip">
<Grid></Grid>
</Window>
I know it's possible to just change ResizeMode="CanResize" but this is the only way to resize the window that I can think of.
Yes you can take out the default resize grip from the Window. You need to override the WindowTemplate. Here is how you would do in XAML:
<Window x:Class="MyNamespace.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None"
ResizeMode="CanResizeWithGrip"
AllowsTransparency="True">
<Window.Template>
<ControlTemplate>
<Border>
</Border>
</ControlTemplate>
</Window.Template>
</Window>
Within the "Border" will go everything that you need to display in the Window (e.g. a TextBox). Then if you want your custom ResizeGrip you can define that as well. Hope this helps.

Categories

Resources