WPF Horizontal alignement stretch but cannot set Window width to 100% - wpf

I have a simple Window that i would like to have a 100% width. I tried the "Stretch" property for Horizontal alignement but width is still stuck to 768.
<Window x:Class="WPF.View.MetroMsgBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MetroMsgBox" ShowInTaskbar="True"
WindowStyle="None" ResizeMode="NoResize"
FontFamily="Segoe UI" Background="{x:Null}"
AllowsTransparency="True"
Height="135" WindowStartupLocation="CenterScreen"
Deactivated="MetroMsgBox_OnDeactivated" HorizontalAlignment="Stretch" >

Hi could you just try that :
Xaml
<Window x:Class="WPF.View.MetroMsgBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MetroMsgBox" ShowInTaskbar="True"
Name=_window
WindowStyle="None" ResizeMode="NoResize"
FontFamily="Segoe UI" Background="{x:Null}"
AllowsTransparency="True"
Height="135" WindowStartupLocation="CenterScreen"
Deactivated="MetroMsgBox_OnDeactivated" HorizontalAlignment="Stretch" >
Code Behind
InitializeComponent();
_window.Width = Screen.PrimaryScreen.Bounds.Width;

Related

How to make Grid with round corner in WPF

I want to make my Grid with round corner in my WPF project.
And due to some reason, I have to use ViewBox and hence to make it more difficult.
My code is as below:
<Window x:Class="WpfApp5.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:WpfApp5"
mc:Ignorable="d"
WindowStyle="None"
AllowsTransparency="True"
Background="Green"
Width="500" Height="300">
<Grid x:Name="gridTotal">
<Viewbox x:Name="vb">
<Grid Width="500" Height="300">
<Image x:Name="BlackMaskImage" Stretch="UniformToFill"/>
<Button Width="100" Height="100">
</Button>
</Grid>
</Viewbox>
</Grid>
I tries some method, like this:
How can I make a rounded-corners form in WPF?
But not work for my case.
Moreļ¼š
In my code, there are two Grid. But for me, the final appearance of the window has 4 round corner is OK. That is I don't care which Grid is fabricated.
I tried to change the gridTotal to a Border, and that border can own round corner. But its content is still a rectangle with sharp right angle.
In UWP, a Grid can apply Style with setter to set its CornerRadius, but in WPF I cannot do so.
You don't specify any region breakdown in the Grid.
Use Border instead.
<Window x:Class="WpfApp5.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"
mc:Ignorable="d"
WindowStyle="None"
AllowsTransparency="True"
Background="{x:Null}"
Width="500" Height="300">
<Border x:Name="gridTotal" CornerRadius="20"
Background="Green">
<Grid Width="500" Height="300">
<Image x:Name="BlackMaskImage" Stretch="UniformToFill"/>
<Button Width="100" Height="100">
</Button>
</Grid>
</Border>
</Window>

Set grid size to windows size

I have the following code to fit the grid exactly to the window.
As you can see in the screenshot, the grid is running out of the window on the bottom and right side. Do you guys have any idea why this is happening? and how to do it properly?
<Window x:Class="WpfApp1.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"
mc:Ignorable="d"
Topmost="true"
Background="Aqua"
WindowStartupLocation="CenterScreen"
DataContext="{Binding Main, Source={StaticResource Locator}}"
Title="MainWindow" Height="500" Width="500">
<Grid
Width="{Binding ActualWidth, RelativeSource = {RelativeSource AncestorType = {x:Type Window}}}"
Height="{Binding ActualHeight, RelativeSource ={RelativeSource AncestorType = {x:Type Window}}}">
<Border Opacity=".9" BorderBrush="Blue" BorderThickness="2"/>
</Grid>
</Window>
If you bind the actual widths and heights of the window, the Grid will exceed the Window, as the content area is smaller than the window itself. Remove the bindings. The Grid will be sized automatically to occupy the available space if you do not explicitly set a Width and Height.
<Window x:Class="WpfApp1.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"
mc:Ignorable="d"
Topmost="true"
Background="Aqua"
WindowStartupLocation="CenterScreen"
DataContext="{Binding Main, Source={StaticResource Locator}}"
Title="MainWindow" Height="500" Width="500">
<Grid>
<Border Opacity=".9" BorderBrush="Blue" BorderThickness="2"/>
</Grid>
</Window>

WPF why is TextBox in WrapPanel is cutted

XAML:
<WrapPanel>
<TextBox ScrollViewer.VerticalScrollBarVisibility="Auto" AcceptsReturn="True"/>
<TextBox ScrollViewer.VerticalScrollBarVisibility="Auto" AcceptsReturn="True"/>
</WrapPanel>
As you can see at the following picture, the second TextBox is cut after it wrapped, to second line.
Picture:
Put the WrapPanel in a ScrollViewer if you want to be able to scroll its content:
<Window x:Class="WpfApplication1.Window1"
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"
Title="Window1" Height="300" Width="300">
<ScrollViewer>
<WrapPanel>
<TextBox Width="400"></TextBox>
<TextBox Height="500" ScrollViewer.VerticalScrollBarVisibility="Visible"></TextBox>
</WrapPanel>
</ScrollViewer>
</Window>
The WrapPanel won't automatically adopt to the size of the window.

WPF I can not get MouseMove event when the form AllowsTransparency=true

<Window x:Class="mtWPFScratchPad.DeskForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="mtWPFScratchPad" AllowsTransparency="True" WindowState="Maximized" Loaded="Window_Loaded" WindowStyle="None" Background="#00FFFFFF" Closing="Window_Closing" Topmost="True" MouseMove="Window_MouseMove">
<InkCanvas Name="inkCanv" Background="Transparent">
</InkCanvas>
When i set the window should be transparency.I can not get the mousemove Event.
How to do this?
If you set Both transparent it's not working You have to change ink canvas color or either change Allow transparency = false
<Window x:Class="Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" AllowsTransparency="True" WindowState="Normal Loaded="Window_Loaded_1" WindowStyle="None" Background="#00FFFFFF" Closing="Window_Closing_1" Topmost="True" MouseMove="Window_MouseMove_1" Height="350" Width="525" >
<Grid>
<InkCanvas Name="inkCanvas" Background="White">
</InkCanvas>
</Grid>
</Window>

WPF Window border issue

I have the following for the main window
<Window x:Uid="Window_1" x:Class="App1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Closing="Window_Closing"
SizeChanged="Window_SizeChanged" Topmost="True" AllowsTransparency="True" Opacity="1"
WindowStyle="None" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen"
WindowState="Maximized" KeyDown="Window_KeyDown" MouseDoubleClick="Window_MouseDoubleClick" Background="Black"
MouseLeftButtonDown="Window_MouseLeftButtonDown" MouseMove="Window_MouseMove" >
<Viewbox x:Uid="Viewbox_1" Stretch="Uniform" StretchDirection="Both" SnapsToDevicePixels="True" >
<Grid x:Uid="MainLayout" Name="MainLayout" SnapsToDevicePixels="True">
<Canvas x:Uid="MainCanvas" Name="MainCanvas" SnapsToDevicePixels="True">
</Canvas>
</Grid>
</Viewbox>
but somehow in the NORMAL mode of the window some padding appears (It is black because of the window Background color)
Any clue how to take it out?
On your root window: Background="Transparent"
I tried to duplicate your problem, but without the full code, it doesn't seem to happen. I used this code:
<Window x:Class="WpfApplication2.MainWindow"
x:Uid="Window_1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Topmost="True" AllowsTransparency="True" Opacity="1"
WindowStyle="None" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" Background="Black" >
<Grid Background="Green">
<Viewbox x:Uid="Viewbox_1" Stretch="Uniform" StretchDirection="Both" SnapsToDevicePixels="True" >
<Grid Background="Red" x:Uid="MainLayout" Name="MainLayout" SnapsToDevicePixels="True">
<Canvas x:Uid="MainCanvas" Name="MainCanvas" SnapsToDevicePixels="True" Background="Blue">
</Canvas>
</Grid>
</Viewbox>
</Grid>
</Window>
Just to see what would happen. Obviously without the image, the inner canvas, grid and viewbox don't take up any space and so all you get is a solid Green window (with the Green from the main Grid completely overlapping the Black of the Window).
My only thought is: Could it have something to do with the aspect ratio of your image versus the aspect ratio of your window when the Viewbox is using Uniform stretching?

Resources