Image is not stretched to the whole client area - wpf

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>

Related

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>

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/

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

TextBlock brush giving incorrect color

I have a TextBlock and a Rectangle, both sitting in an empty WPF4 window. The TextBlock's Foreground and the Rectangle's Fill are both set to a SolidColorBrush with value #80800000.
This is what it looks like:
The Rectangle's color is correct (a 50% transparent maroon) but the TextBlock gives a flat grey. What's going on?
EDIT: Here's the XAML:
<Window x:Class="WpfApplication1.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>
<TextBlock Foreground="#80800000" Height="100" HorizontalAlignment="Left" Margin="47,39,0,0" Text="TextBlock" VerticalAlignment="Top" Width="266" FontFamily="Arial" FontWeight="Bold" FontSize="56" />
<Rectangle Fill="#80800000" Height="100" HorizontalAlignment="Left" Margin="71,174,0,0" Stroke="{x:Null}" VerticalAlignment="Top" Width="200" />
</Grid>
</Window>
Has this something to do with any of the additional icons you have at the top of your window?
This code as expected for me.
screenshot
or are there any additional opacity setting in your code behind?

Resources