Round Corner Window in WPF - 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>

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>

Control's scroll bar hidden by border element when adjusting window's size

I have a WPF window which I have tried to add scrolls bar to, however the scroll bars get overlapped by the Border element unless I resize the window exactly.
I have attached some pictures to show you what is happening. Can anyone help me solve this graphical error so that the scroll bars are never overlapped by whatever?
And here is the code for the WPF window:
<Window x:Class="TrackerClient.BITDisplayWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="System Status (Built-In Test)"
MaxHeight="{x:Static SystemParameters.PrimaryScreenHeight}"
MaxWidth="{x:Static SystemParameters.PrimaryScreenWidth}"
Height="450" Width="900"
WindowStartupLocation="CenterOwner"
Loaded="Window_Loaded" Closed="Window_Closed">
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Auto" Style="{DynamicResource ResourceKey=styleScrollViewer}" VerticalScrollBarVisibility="Auto" Grid.Column="0">
<Border Name="m_hostElement" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Stretch" Height="auto" VerticalAlignment="Stretch" Width="auto" Margin="10" Grid.Column="1"/>
</ScrollViewer>
</Grid>
</Window>

How do I get a TabControl to use the full width of its parent in wpf?

I have a tab control inside canvas. How do I get a TabControl to use the full width of its parent?
<UserControl
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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SilverlightApplication1.MainPage"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="White" Margin="0,0,0,0">
<sdk:TabControl Width="400" Height="300" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="0,0" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" >
<sdk:TabItem Header="myResponse">
<Grid Background="#FFE5E5E5"/>
</sdk:TabItem>
<sdk:TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</sdk:TabItem>
</sdk:TabControl>
Without talking about why you are using a canvas in the first place, I would recommend you read about width and height properties in WPF.
In your example, the width is set specifically to a defined amount of pixel. With this, the TabControl will not scale. You should use relative values like * and auto and work with margins where they are needed. This tutorial could be a start: http://www.c-sharpcorner.com/UploadFile/mahesh/wpf-layout-size-width-and-height/

Image is not stretched to the whole client area

Visual Studio 2012, WPF C# Windows Application. XAML:
<Window x:Class="Edge.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 Background="Blue">
<Image Source="Images/House.png"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stretch="Fill" Margin="3"
/>
</Grid>
</Window>
Image is not stretched to the whole window, as I want, it is shown only in 1/4 window area. I expect that Stretch="Fill" must stretch the image to the whole window. What is wrong?
The problem is your outer Grid. A Grid will re-size to fit the size of its children, but the child in this case (the Image) is trying to stretch to fill the size of its parent so I'm going to guess that what you are seeing is the image being drawn actual size.
Try using a DockPanel instead which fills all available parent space children.
<Window x:Class="Edge.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">
<DockPanel Background="Blue">
<Image Source="Images/House.png"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stretch="Fill" Margin="3"/>
</DockPanel>
</Window>

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