WPF Window border issue - wpf

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?

Related

Change background opacity on startup

I would like to change background opacity (make it darker) when programs starts
<Window x:Class="PCSX4.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:PCSX4"
mc:Ignorable="d"
Title="MainWindow" Height="900" Width="900"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
Background="Transparent">
<WindowChrome.WindowChrome>
<WindowChrome GlassFrameThickness="0,0,0,1" CornerRadius="0" />
</WindowChrome.WindowChrome>
<Grid>
<Border Background="lightBlue" />
<Image HorizontalAlignment="Center" Height="1002" VerticalAlignment="Center" Width="900" RenderTransformOrigin="-2.3,-0.484" Source="/background.png" Stretch="Fill"/>
</Grid>
</Window>
Haven't tried anything because I don't know how to solve this problem as I'm new to this.

Round Corner Window in WPF

I am trying window corner rounded in wpf but still Grid corner showing .
Looks like this type of issue:
https://www.google.com/imgres?imgurl=http%3A%2F%2Fdontpaniclabs.com%2Fwp-content%2Fuploads%2F2014%2F12%2Ftransparent_WPF1.jpg&imgrefurl=https%3A%2F%2Fdontpaniclabs.com%2Fblog%2Fpost%2F2014%2F10%2F30%2Frounded-corners-with-transparent-backgrounds-in-wpf%2F&tbnid=TD_RmzKgFCi6LM&vet=12ahUKEwjeqNKH_ZL0AhU4KLcAHXkaDwAQMygBegUIARCwAQ..i&docid=Q-nsUFZ1FM7sAM&w=550&h=367&q=round%20window%20corners%20wpf&ved=2ahUKEwjeqNKH_ZL0AhU4KLcAHXkaDwAQMygBegUIARCwAQ
I am using this style.
You should make you window borderless (with WindowStyle="None") and transparent (with AllowsTransparency="True" and Background="Transparent"). Then add Border control and set CornerRadius property with desired value and Background property with desired color. As you can't put all Window content into Border control, you should add another sub-Grid for all Window elements and put sub-Grid and Border into main Grid.
XAML:
<Window x:Class="WPFApp.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:WPFApp"
mc:Ignorable="d"
Title="MainWindow"
Width="400"
Height="200"
WindowStyle="None"
Background="Transparent"
AllowsTransparency="True">
<Grid>
<Border CornerRadius="25"
Background="White" />
<Grid x:Name="Content"
Margin="10">
<!-- Put all window content here -->
<Label Content="CONTENT HERE"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
FontSize="24"/>
</Grid>
</Grid>
</Window>

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>

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

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;

Rounded Corners on Canvas dissappear when Element is dragged to the border

i have this code:
<Window x:Class="WpfApplication8.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">
<Grid>
<ScrollViewer Background="#FFADB9CD" >
<Grid>
<Border Name="mask" Height="{Binding ElementName=cnvsEtikett, Path=Height}" Width="{Binding ElementName=cnvsEtikett, Path=Width}" Background="White" CornerRadius="6"/>
<Canvas Height="100" Name="cnvsEtikett" Width="200" Background="White" ClipToBounds="True">
<Canvas.OpacityMask>
<VisualBrush Visual="{Binding ElementName=mask}" />
</Canvas.OpacityMask>
<TextBlock Height="23.2" Text="TextBlock" Canvas.Left="63" Canvas.Top="41" />
</Canvas>
</Grid>
</ScrollViewer>
</Grid>
</Window>
I want the canvas to have rounded corners, which works until i drag the textblock to either side. then the corners dissappear. this is a simple recreation of the window in my programm, there i use drag and drop operations to move the textblock. I really need those rounded corners, but i am absolutely clueless of how i could solve this problem!
any ideas??
edit: it seems as if the canvas is stretched as the textblock is moved to the edge ( because the corner radius changes too!)
Just thought of another solution..
<Border ClipToBounds="True" CornerRadius="6" Background="White" Name="brdEtikett" Height="200" Width="200" >
<Canvas Name="cnvsEtikett" Background="Transparent" />
</Border>
set the background of the border to the color you want on the canvas and make the canvas transparent!!
this way you don't have to put padding or margin on the elements!
Best bet is probably to put the canvas inside the border (I typically put a small margin on panels inside of rounded corner borders to keep stuff from sticking out). This way, the border will expand or contract automatically as the canvas does (provided it is set to auto height width).
Border can have a single content child, in this case the Canvas with content of its own.
EDIT
Doesn't this achieve the same thing?
<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="WpfApplication8.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ScrollViewer Background="#FFADB9CD" >
<Grid>
<Border x:Name="mask" Background="White" CornerRadius="6" HorizontalAlignment="Center" VerticalAlignment="Center">
<Canvas Height="100" x:Name="cnvsEtikett" Width="200" ClipToBounds="True" d:LayoutOverrides="Margin">
<TextBlock Height="23.2" Text="TextBlock" Canvas.Left="59" Canvas.Top="31" />
</Canvas>
</Border>
</Grid>
</ScrollViewer>
</Grid>
</Window>
Gong Solutions Drag Drop Helper library

Resources