WPF Border ZIndex not working as expected - wpf

I have the following problem: I overlay 2 controls over one another and want a single border to surround them both. I thought I could control the ZIndex of the border over each control and give them a low ZIndex: 2. Then give the controls a higher ZIndex (4 and 5). The following xaml as a UserControl is what I'm referring to:
<Canvas>
<Border BorderBrush="Black" BorderThickness="4" Canvas.ZIndex="2" Canvas.Left="50" Canvas.Top="30">
<Rectangle Width="200" Height="20" Fill="Aqua" Canvas.ZIndex="5"/>
</Border>
<Border BorderBrush="Black" BorderThickness="4" Canvas.ZIndex="2" Canvas.Left="150" Canvas.Top="00">
<Rectangle Width="50" Height="200" Fill="Yellow" Canvas.ZIndex="5"/>
</Border>
</Canvas>
The result looks like:
But I desire is the following image but with the border with red-X removed:
Can anyone recommend a way to do this ? The Border ZIndex method is not working.
Thanks!

ZIndex is not working cause Rectangles are not childs of Canvas. So you can fix it by placing them outside of borders and adjusting its Heights, Widths and Canvas.Left, Canvas.Top properties. So the example looks like this.
<Canvas>
<Border BorderBrush="Black" BorderThickness="4" Canvas.ZIndex="2"
Canvas.Left="46" Canvas.Top="26"
Width="208" Height="28">
</Border>
<Rectangle Width="200" Height="20" Fill="Aqua" Canvas.ZIndex="5"
Canvas.Left="50" Canvas.Top="30" />
<Border BorderBrush="Black" Height="208" Width="58" BorderThickness="4"
Canvas.ZIndex="2" Canvas.Left="146" Canvas.Top="00">
</Border>
<Rectangle Canvas.Left="150" Canvas.Top="4" Width="50" Height="200" Fill="Yellow"
Canvas.ZIndex="5"/>
</Canvas>

Related

Draw Image Outside of Layout in WPF

I'm creating a style to customize a RadioButton so that the RadioButton can display a star image behind the Bullet. I have the star being drawn with an ImageBrush as the background of a 20x20 grid that holds the Bullet layout. It's working fine, except I don't want the overall height of the RadioButton to be 20px tall. So I want to make the grid only 10x10 but still have the star 20x20 and centered behind the bullet (therefore the star's top left coordinate would be -5,-5 relative to the top left of grdBullet). How can I draw the star image behind or outside of the layout?
Excerpt of my style:
....
<ImageBrush x:Key="StarBrush" ImageSource="/Common;component/Resources/Images/FavoriteStar_FrontFacing_24x24_96.png" />
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<BulletDecorator VerticalAlignment="Center">
<BulletDecorator.Bullet>
<Grid Name="grdBullet" Height="20" Width="20" Background="{StaticResource StarBrush}">
<Grid Width="10" Height="10" HorizontalAlignment="Center" VerticalAlignment="Center">
<Ellipse Name="RadioOuter" Fill="#FFF4F4F4" Stroke="#FF8E8F8F" StrokeThickness="1"/>
<Ellipse Name="RadioInner" StrokeThickness="1" Margin="2" Fill="{StaticResource RadioInnerDefaultFill}" Stroke="{StaticResource RadioInnerDefaultStroke}" />
<Ellipse Name="RadioChecked" StrokeThickness=".75" Margin="2.5" Stroke="#FF193B55" Fill="{StaticResource RadioCheckedFill}" Visibility="Hidden" />
<Border CornerRadius="0" Margin="4" Name="RadioMark" Background="#FFADADAD" Visibility="Hidden" />
</Grid>
</Grid>
</BulletDecorator.Bullet>
<!--Text element-->
<TextBlock Margin="3,0,0,0" VerticalAlignment="Center" Foreground="{TemplateBinding Foreground}" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}">
<ContentPresenter />
</TextBlock>
</BulletDecorator>
....
Two words: Negative margins.
<BulletDecorator.Bullet>
<Grid Width="10" Height="10" HorizontalAlignment="Center" VerticalAlignment="Center">
<Rectangle Height="20" Width="20" Margin="-5" Fill="{StaticResource StarBrush}" />
...
</Grid>
</BulletDecorator.Bullet>
For a more general purpose, you could set the Margin using a MultiValueConverter that returns a Thickness. For example, Margin.Left would be equal to -(SelfWidth - ParentWidth) / 2.
You can render anything anywhere with negative margins, no need for canvas. Try this (i have assumed -5,5 is center of image otherwise you need to put -15 15 or even -25 25
<ImageBrush x:Key="StarBrush"
ImageSource="/Common;component/Resources/Images/FavoriteStar_FrontFacing_24x24_96.png"
Margin="-5 5 0 0" />

How can I set opacity of a textbox to 1 when it is within a border with opacity 0.5?

I have the following xaml:
<Border x:Name="baseBorder" Grid.Row="0" Grid.RowSpan="200" Canvas.ZIndex="1" Opacity="0.5" Background="Gray">
<Border x:Name="interiorBorder" Background="White" Height="200" Width="450" Opacity="1">
<TextBlock x:Name="txtMessage" HorizontalAlignment="Center" Width="400" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" FontSize="20" FontWeight="Bold" Foreground="Black" >
</Border>
</Border>
I want the interiorBorder to have opacity 1 while maintaining the base border's opacity 0.5.
The above xaml is not working, I'm getting 0.5 opacity in both objects.
Try making interiorBorder a sibling of baseBorder instead of a child, perhaps something like this.
<Border x:Name="baseBorder" Grid.Row="0" Grid.RowSpan="200" Canvas.ZIndex="1" Opacity="0.5" />
<Border x:Name="interiorBorder" Grid.Row="0" Grid.RowSpan="200" Canvas.ZIndex="1" Opacity="1">
<TextBlock x:Name="txtMessage" />
</Border>
I imagine you'll also want to set Margin on interiorBorder so you'll still be able to see baseBorder.

Different Color to each side in Border in WPF XAML?

I want to have different color to each side of a border in WPF XAML. How can i do this.
<Border BorderThickness="1,2,3,4" BorderBrush="Blue"></Border>
A bit hacky, but it works.
<Grid>
<Border BorderThickness="1,0,0,0" BorderBrush="Blue"/>
<Border BorderThickness="0,2,0,0" BorderBrush="Red"/>
<Border BorderThickness="0,0,3,0" BorderBrush="Green"/>
<Border BorderThickness="0,0,0,4" BorderBrush="Orange"/>
</Grid>
Probably better to create your own Decorator.
Maybe?
<DockPanel LastChildFill="True">
<Rectangle Fill="Red" DockPanel.Dock="Top" Height="2"/>
<Rectangle Fill="Yellow" DockPanel.Dock="Left" Width="2"/>
<Rectangle Fill="Green" DockPanel.Dock="Right" Width="2"/>
<Rectangle Fill="Blue" DockPanel.Dock="Bottom" Height="2"/>
<Rectangle Fill="Wheat"/>
</DockPanel>
There is a hacky way that using four Border https://stackoverflow.com/a/1797045/5229294
<Border BorderThickness="0,0,0,10" BorderBrush="Green">
<Border BorderThickness="0,0,10,0" BorderBrush="Blue">
<Grid>
<Button>Hello</Button>
</Grid>
</Border>
</Border>

WPF : Rounded-Corners Images

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Black">
<!-- Rounded yellow border -->
<Border BorderThickness="3" BorderBrush="Yellow" CornerRadius="10" Padding="2"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid>
<!-- Rounded mask (stretches to fill Grid) -->
<Border Name="mask" Background="White" CornerRadius="7"/>
<!-- Main content container -->
<StackPanel>
<!-- Use a VisualBrush of 'mask' as the opacity mask -->
<StackPanel.OpacityMask>
<VisualBrush Visual="{Binding ElementName=mask}"/>
</StackPanel.OpacityMask>
<!-- Any content -->
<Image Source="http://chriscavanagh.files.wordpress.com/2006/12/chriss-blog-banner.jpg"/>
<Rectangle Height="50" Fill="Red"/>
<Rectangle Height="50" Fill="White"/>
<Rectangle Height="50" Fill="Blue"/>
</StackPanel>
</Grid>
</Border>
</Page>
This XAML is from WPF – Easy rounded corners for anything but it doesn't work form me =(
<Border Canvas.Left="55"
Canvas.Top="30"
Width="100"
Height="Auto"
Margin="12,12,8,0"
VerticalAlignment="Top"
BorderBrush="#FF3B5998"
BorderThickness=".5"
CornerRadius="18">
<Border.Effect>
<DropShadowEffect BlurRadius="5"
Opacity=".5"
ShadowDepth="3" />
</Border.Effect>
<Border Name="ReceiverColor"
BorderBrush="#FF96B2E4"
BorderThickness="6"
CornerRadius="15">
<Border Name="Mask"
BorderBrush="#FF3B5998"
BorderThickness=".5"
CornerRadius="13">
<StackPanel>
<StackPanel.OpacityMask>
<VisualBrush Visual="{Binding ElementName=Mask}" />
</StackPanel.OpacityMask>
<Image Name="Receiver" />
</StackPanel>
</Border>
</Border>
</Border>
--- EDIT ---
I make borders sizes to auto and change source of image to an image from a link
when window loaded border size becomes as image size but image not shown !!!
You can define a <Border/> element and set its <Border.Background/>
property to an <ImageBrush/> , set the Borders CornerRadius property and you
are all set!
<Border CornerRadius="8,0,8,0">
<Border.Background>
<ImageBrush Stretch="Fill" ImageSource="ImageSource"/>
</Border.Background>
</Border>
You forgot the Grid that makes the mask and the image siblings and nested the image in the mask. and you forgot to set the background of the mask.
This works:
<Grid>
<Border Canvas.Left="55"
Canvas.Top="30"
Width="100"
Height="Auto"
Margin="12,12,8,0"
VerticalAlignment="Top"
BorderBrush="#FF3B5998"
BorderThickness=".5"
CornerRadius="18">
<Border.Effect>
<DropShadowEffect BlurRadius="5"
Opacity=".5"
ShadowDepth="3" />
</Border.Effect>
<Border Name="ReceiverColor"
BorderBrush="#FF96B2E4"
BorderThickness="6"
CornerRadius="15">
<Grid>
<Border Name="Mask"
Background="White"
BorderBrush="#FF3B5998"
BorderThickness=".5"
CornerRadius="13">
</Border>
<StackPanel>
<Image Name="Receiver"
Source="/Images/test.jpg" />
<StackPanel.OpacityMask>
<VisualBrush Visual="{Binding ElementName=Mask}" />
</StackPanel.OpacityMask>
</StackPanel>
</Grid>
</Border>
</Border>
</Grid>
in wpf this one works for me
<Ellipse Width="50" Height="50">
<Ellipse.Fill>
<ImageBrush ImageSource="http://chriscavanagh.files.wordpress.com/2006/12/chriss-blog-banner.jpg" />
</Ellipse.Fill>
</Ellipse>
None of the above answers worked for me completely. I was trying to implement rounded corners on image which could be resized and has properties Stretch="UniformToFill" VerticalAlignment="Center" and HorizontalAlignment="Center".
The center alignments keeps the middle part cropped as opposed to bottom and right side being cropped, when image is resized. Solutions with image brush were working but I was facing problem in keeping the content at center cropped.
The marked answer has a problem with transparent non rectangular images as the "mask" border will end up showing as white background. Following was the imlementation which worked for me:
<Grid>
<WrapPanel Name ="container">
<Image Source="sample_image.png" VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="UniformToFill"/>
<WrapPanel.OpacityMask>
<VisualBrush >
<VisualBrush.Visual>
<Border Height="{Binding ElementName=container, Path=ActualHeight}"
Width="{Binding ElementName=container, Path=ActualWidth}"
Background="White" CornerRadius="15" />
</VisualBrush.Visual>
</VisualBrush>
</WrapPanel.OpacityMask>
</WrapPanel>
</Grid>
You can use an ellipse like how Usman Ali has said (I thought this myself and I didn't take it from him)
It's very simple, make an ellipse with the properties you want, then set the fill to an imagebrush with your desired image like this in XAML:
<Ellipse Height="Auto" Width="100">
<Ellipse.Fill>
<ImageBrush ImageSource="YOUR IMAGE SOURCE/LINK HERE"/>
</Ellipse.Fill>
</Ellipse>
In C#, if in any case you want to do in C#:
Ellipse YourEllipseName = new Ellipse
{
Height = 50,
Width = 50,
StrokeThickness = 0,
Fill = new ImageBrush
{
Stretch = Stretch.Uniform,
ImageSource = new BitmapImage(new Uri("YOUR IMAGE SOURCE HERE"))
}
};
<Grid Background="Black">
<Rectangle RadiusX="20" RadiusY="20"
Width="130"
Height="130">
<Rectangle.Fill>
<ImageBrush x:Name="myImage" ImageSource="C:\Path\Desktop\visual-studio-2010-logo.png"/>
</Rectangle.Fill>
</Rectangle>
</Grid>

WPF Rounded Corners background bleeding through

I'm making my first foray into WPF - I have a simple form with a popup defined for inline help. I'm using rounded corners, and for some reason a black background is bleeding through around the corners. I don't understand which element is causing the problem.
alt text http://www.awbrey.net/rounded.jpg
I assume it's something blindingly obvious which I'm just not seeing. Here's the XAML I'm using:
<Window x:Class="Consent.Client.SubjectNumberEntry"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" FontSize="24"
Title="SubjectNumberEntry" WindowStyle="None" WindowState="Maximized"
xmlns:h="clr-namespace:Consent.Client" KeyDown="windowOuter_KeyDown" Background="White" Name="windowOuter" AllowsTransparency="true" Loaded="Window_Loaded">
<StackPanel Height="400" DockPanel.Dock="Top" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10">
<StackPanel Height="60" Orientation="Horizontal" VerticalAlignment="Center">
<TextBox Name="txtSubjectNumber" Margin="10" Width="400" KeyDown="txtSubjectNumber_KeyDown" h:HelpProvider.HelpString="Enter the subject identifier, or scan their wristband">
<TextBox.ToolTip>This is a textbox</TextBox.ToolTip>
</TextBox>
<Button Name="btnEnter" Margin="10" Width="100" Click="btnEnter_Click">Enter</Button>
<Button Width="50" Name="btnHelp" Margin="10" Click="btnHelp_Click">?</Button>
<Button Width="50" Name="btnExit" Margin="10" Click="btnExit_Click">Exit</Button>
</StackPanel>
<Label Name="lblValue" Margin="10"></Label>
<Popup Placement="Bottom" HorizontalAlignment="Center" VerticalOffset="10" MouseDown="popHelp_MouseDown" PopupAnimation="Fade" Name="popHelp" PlacementTarget="{Binding ElementName=txtSubjectNumber}">
<Border Padding="10" Margin="10" BorderBrush="CornflowerBlue" BorderThickness="1" CornerRadius="10" Background="CornflowerBlue">
<TextBlock FontSize="12" Background="CornflowerBlue">This is the content of the help box.</TextBlock>
</Border>
</Popup>
</StackPanel>
</Window>
I think it is the Popup that is causing the problem. Try setting AllowsTransparency to True on the popup.
Popup.AllowsTransparency
When set to False, any transparent colors are "merged" with black.
You can also wrap the popup in a border that has rounded corners. This is useful if you can't change the AllowsTransparency of the popup.
Something like this:
<Border CornerRadius="10">
<Popup Placement="Bottom" HorizontalAlignment="Center" VerticalOffset="10" MouseDown="popHelp_MouseDown" PopupAnimation="Fade" Name="popHelp" PlacementTarget="{Binding ElementName=txtSubjectNumber}">
<Border Padding="10" Margin="10" BorderBrush="CornflowerBlue" BorderThickness="1" CornerRadius="10" Background="CornflowerBlue">
<TextBlock FontSize="12" Background="CornflowerBlue">This is the content of the help box.</TextBlock>
</Border>
</Popup>
</Border>

Resources