WPF Rounded Corners background bleeding through - wpf

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>

Related

Two dockpanels inside another dockpanel

How to get these dockpanels right ?
<DockPanel Grid.Row="1" LastChildFill="True" HorizontalAlignment="Stretch">
<DockPanel Width="400" LastChildFill="False" HorizontalAlignment="Left">
<DockPanel>
<TextBlock Width="400" />
</DockPanel>
<DockPanel Height="35" DockPanel.Dock="Bottom" LastChildFill="False">
<Button x:Name="btnRefresh" Content="Refersh" />
</DockPanel>
</DockPanel>
The DockPanel with the TextBlock spans over the DockPanel that is docked at the bottom, I want it to fit right up to it. Any ideas?
Ok, it turns out: the panel docked at the bottom must preceed the dockpanel above it in the xaml declaration. LastChildFill="True" applies to the control that is declared last in the code.
<DockPanel Grid.Row="1" LastChildFill="True" HorizontalAlignment="Stretch">
<DockPanel Width="400" LastChildFill="False" HorizontalAlignment="Left">
<DockPanel Height="35" DockPanel.Dock="Bottom" LastChildFill="False">
<Button x:Name="btnRefresh" Content="Refersh" />
</DockPanel>
<DockPanel>
<TextBlock Width="400" />
</DockPanel>
</DockPanel>
Please refer to the DockPanel Class page at MSDN which has all the help that you need. The XAML example from the linked page:
<DockPanel LastChildFill="True">
<Border Height="25" Background="SkyBlue" BorderBrush="Black" BorderThickness="1"
DockPanel.Dock="Top">
<TextBlock Foreground="Black">Dock = "Top"</TextBlock>
</Border>
<Border Height="25" Background="Blue" BorderBrush="Black" BorderThickness="1"
DockPanel.Dock="Top">
<TextBlock Foreground="White">Dock = "Top"</TextBlock>
</Border>
<Border Height="25" Background="Yellow" BorderBrush="Black" BorderThickness="1"
DockPanel.Dock="Bottom">
<TextBlock Foreground="Black">Dock = "Bottom"</TextBlock>
</Border>
<Border Width="200" Background="PaleGreen" BorderBrush="Black" BorderThickness="1"
DockPanel.Dock="Left">
<TextBlock Foreground="Black">Dock = "Left"</TextBlock>
</Border>
<Border Background="White" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black">This will fill the remaining space</TextBlock>
</Border>
</DockPanel>
Note the use of the DockPanel.Dock attached properties.
<DockPanel Grid.Row="1" LastChildFill="True" HorizontalAlignment="Stretch">
<DockPanel Width="400" LastChildFill="False" HorizontalAlignment="Left">
<Button x:Name="btnRefresh" Content="Refersh"
Height="35" DockPanel.Dock="Bottom" />
<TextBlock Width="400" />
</DockPanel>
<!-- Other UI Elements here? -->
<DockPanel>

WPF Modal Window Transparency

I have created a modal WPF window that looks as follows:
Here is the code for the window:
<Window x:Class="Dionysus.Core.Controls.ModalWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ModalWindow" AllowsTransparency="True" Background="Transparent" WindowStyle="None">
<Grid Name="MainGrid">
<Rectangle Fill="Gray" Opacity="0.7" />
</Grid>
The "ErrorControl" is then added as follows:
MainGrid.Children.Add(uc);
The problem is as soon as I expand the stack trace, the controls transparency also changes:
I am assuming this has something to do with the ScrollViewer that uses the incorrect transparency, ie of the Rectangle instead of the containing Window.
I have also set the Opacity of the UserControl which owns the ScrollViewer to 1 and then binded the Opacity:
<ScrollViewer Background="WhiteSmoke" Opacity="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=Opacity}">
Can anyone help me?
--
UPDATE
Here is the code for the UserControl that is inserted into the Window
<Grid x:Name="LayoutRootx" Background="WhiteSmoke">
<StackPanel VerticalAlignment="Stretch">
<TextBlock TextWrapping="Wrap" Margin="5" Text="An error has occured:" Foreground="Black" FontSize="15" FontWeight="Medium"/>
<TextBlock TextWrapping="Wrap" Margin="5,10,5,5" Text="{Binding Error}"/>
<odc:OdcExpander Header="Stack Trace" Margin="5" IsExpanded="False" Background="WhiteSmoke">
<TextBox Text="{Binding StackTrace}" TextWrapping="Wrap" Margin="5,10,5,5" IsReadOnly="True" MaxHeight="370"/>
</odc:OdcExpander>
<odc:OdcExpander Header="Comment" Margin="5" IsExpanded="False">
<TextBox Text="{Binding Comment}" TextWrapping="Wrap" Margin="5,10,5,5" MaxHeight="370" Name="txtComment"/>
</odc:OdcExpander>
<StackPanel Margin="5,10,5,5" Orientation="Horizontal" HorizontalAlignment="Left">
<Button Style="{StaticResource DionysusButton}" Width="100" Height="23" IsDefault="True" Name="btnSendError">
<StackPanel Orientation="Horizontal">
<Image Source="/Dionysus.Shell;component/Images/camera-icon.png" Margin="0,0,5,0">
</Image>
<TextBlock Text="Send to IT" VerticalAlignment="Center"/>
<core:DionysusTriggerAction Height="0" Width="0" TargetControl="{Binding ElementName=btnSendError}" MethodName="SendError"></core:DionysusTriggerAction>
</StackPanel>
</Button>
<Button Style="{StaticResource DionysusButton}" Width="100" Height="23" Name="btnExit" Margin="10,0,0,0" IsCancel="True">
<StackPanel Orientation="Horizontal">
<Image Source="/Dionysus.Shell;component/Images/DeleteRed.png" Margin="0,0,5,0">
</Image>
<TextBlock Text="Close" VerticalAlignment="Center"/>
</StackPanel>
</Button>
<core:DionysusTriggerAction Height="0" Name="triggerAction2" Width="0" TargetControl="{Binding ElementName=btnExit}" MethodName="Exit"></core:DionysusTriggerAction>
</StackPanel>
</StackPanel>
</Grid>
If your window has a fixed size and cannot be resized, you can use the following trick:
<Grid>
<Border BorderThickness="100" BorderBrush="Gray" Opacity="0.7">
<Grid Background="White" Grid.Column="1" Grid.Row="1" x:Name="contentPlaceHolder">
<TextBlock Text="HELLO WORLD" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
</Grid>
However, it is unlikely that your Window will always have the same size, so to make it more dynamic, you could change the layout of the Window as follows:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="YourDesiredSize"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="YourDesiredSize"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Rectangle Fill="Gray" Opacity="0.7" Grid.Row="0" Grid.ColumnSpan="3"/>
<Rectangle Fill="Gray" Opacity="0.7" Grid.Row="2" Grid.ColumnSpan="3"/>
<Rectangle Fill="Gray" Opacity="0.7" Grid.Row="1" Grid.Column="0"/>
<Rectangle Fill="Gray" Opacity="0.7" Grid.Row="1" Grid.Column="2"/>
<Grid Grid.Column="1" Grid.Row="1" Background="White" x:Name="contentPlaceHolder">
<TextBlock Text="HELLO WORLD" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Grid>
The result of this window placed on top of another looks more or less like this:
and then instead of adding to the MainGrid, add the UserControl to the contentPlaceHolder or however you want to call it:
contentPlaceHolder.Children.Add(uc);
Okay so I found a solution that works for me, I'm sure it's not the best but it might help someone having the same problem as I did.
The problem was that controls within my UserControl that I added to my Window were transparent, although I could not figure out the reason, I found a simple workaround.
By changing the OpacityMask property of the UserControl to whatever the required Background colour is, even if the controls opacity changes, it will be masked with the Brush that you supply.
uc.OpacityMask = Brushes.WhiteSmoke;
Hope it helps someone!

How do I implement a "Frame effect" in Silverlight

I would like to reproduce this effect :
How can I do with xaml ?
(note that the text can be variable)
Thanks in advance for your help
You can easily achieve this with a combination of Border and Grid panels:
<Grid Width="200" Height="200" VerticalAlignment="Center" HorizontalAlignment="Center">
<Border BorderThickness="1" BorderBrush="Black" Margin="0,7,0,0">
<TextBlock Text="Lorem Ipsum..." Margin="20"/>
</Border>
<Border Background="White" Margin="10,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Top">
<TextBlock Text="My Title" />
</Border>
</Grid>

Stopping WPF TextBox from growing with text

I am trying to modify my simple control so that the text box does not grow as the user types in long text. I took a look at some solutions posted here at Stackoverflow that suggest using a Grid and an invisible Border and binding the Width of the text box to the ActualWidth of the Border, but I can't seem to get it to work in my setup.
Here is the xaml of my control:
<StackPanel Margin="5,0">
<WrapPanel Margin="0,0,0,5">
<TextBlock Foreground="White" Margin="0,0,2,0">TEXT</TextBlock>
<TextBlock Foreground="#FF0099CC" FontWeight="Bold">MORE TEXT</TextBlock>
</WrapPanel>
<Border Margin="2,4,0,4" BorderThickness="1" SnapsToDevicePixels="True" Background="Black"
BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
<StackPanel Orientation="Horizontal">
<Image Source="..\Resources\zoom.png" Width="13"/>
<TextBox Foreground="White" Background="Black" BorderBrush="Transparent">THIS IS SOME TEXT</TextBox>
</StackPanel>
</Border>
</StackPanel>
Any ideas? I need the zoom.png to appear "inside" of the text box so I use a horizontal stack panel and just place the image and text box side by side, both surrounded by the same border.
Is there a way for me to stop my text box from auto growing as text is typed?
Thanks.
UPDATE:
Below is the xaml I am testing with.
<Window x:Class="Desktop.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:composite="http://www.codeplex.com/CompositeWPF"
Title="MyShell" Height="50" Width="900"
WindowStyle="None"
ShowInTaskbar="False"
AllowsTransparency="True"
Background="Transparent"
ResizeMode="CanResizeWithGrip"
WindowStartupLocation="CenterScreen">
<Border BorderBrush="Black"
BorderThickness="1.5"
CornerRadius="5"
Background="Gray">
<ItemsControl composite:RegionManager.RegionName="MainRegion">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel></WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Border>
</Window>
<UserControl x:Class="Desktop.SearchTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="50" Margin="0">
<StackPanel Margin="5,0">
<WrapPanel Margin="0,0,0,5">
<TextBlock Foreground="White" Margin="0,0,2,0">TEXT</TextBlock>
<TextBlock Foreground="#FF0099CC" FontWeight="Bold">MORE TEXT</TextBlock>
</WrapPanel>
<Border Margin="2,4,0,4" BorderThickness="1" SnapsToDevicePixels="True" Background="Black"
BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
<Grid x:Name="grdTest">
<Image HorizontalAlignment="Left" Source="..\Resources\zoom.png" Width="13"/>
<TextBox Margin="16,0,0,0" Foreground="White" Background="Black" BorderBrush="Transparent" Width="{Binding ElementName=grdTest, Path=ActualWidth}">THIS IS SOME TEXT</TextBox>
</Grid>
</Border>
</StackPanel>
</UserControl>
I just add my user control to the Window's MainRegion.
I did some more searching and found the solution. I used the below Grid to replace the grid in my original post and now the text box keeps its original size and does not auto grow on long user input. Thanks to all who looked into this.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image Source="..\Resources\zoom.png" Width="13"/>
<Border x:Name="b" Grid.Column="1"/>
<TextBox Width="{Binding ActualWidth, ElementName=b}" Foreground="White" Background="Black" BorderBrush="Transparent"
Grid.Column="1"
VerticalAlignment="Center"
Text="THIS IS SOME TEXT"/>
</Grid>
Try to put ScrollViewer.HorizontalScrollBarVisibility="Disabled" inside Grid.
Name the Grid as x:Name="grdTest" and try this
<Grid x:Name="grdTest">
<Image HorizontalAlignment="Left" Source="..\Resources\zoom.png" Width="13"/>
<TextBox Margin="16,0,0,0" Foreground="White" Background="Black" BorderBrush="Transparent"
Width="{Binding ElementName=grdTest, Path=ActualWidth}">THIS IS SOME TEXT</TextBox>
</Grid>
To stop the grow behavior set an explicit size to the TextBox or place it in a grid with the HorizontalAlignment set to stretch
Option 1:
<TextBox Width="60">THIS IS SOME TEXT</TextBox>
Option 2:
<TextBox HorizontalAlignment="Stretch">THIS IS SOME TEXT</TextBox>
I had a very similar issue, and it turned out that my Window property "SizeToContent" was set to "WidthAndHeight", I removed setting that property, (which then defaults to "Manual") and this fixed my similar issue.
To scale the border containing the textbox to the width of the outter stack panel, change the inner stack panel to a grid and set a left margin on the text box so it doesn't overlap with the image. Also set the image's horizontal alignment to left (if that is where you want it) or it will default to the center of the border.
<StackPanel Margin="5,0">
<WrapPanel Margin="0,0,0,5">
<TextBlock Foreground="White" Margin="0,0,2,0">TEXT</TextBlock>
<TextBlock Foreground="#FF0099CC" FontWeight="Bold">MORE TEXT</TextBlock>
</WrapPanel>
<Border Margin="2,4,0,4" BorderThickness="1" SnapsToDevicePixels="True" Background="Black"
BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
<Grid>
<Image HorizontalAlignment="Left" Source="..\Resources\zoom.png" Width="13"/>
<TextBox Margin="16,0,0,0" Foreground="White" Background="Black" BorderBrush="Transparent">THIS IS SOME TEXT</TextBox>
</Grid>
</Border>
</StackPanel>
This article shows a solution for a textbox that is displayed inside a scrollviewer's viewport (in treeview or listbox). A PART_MeasureTextBlock is used (bound to the textbox) to determine the available size and set it to the textbox.
Please have look here and tell me what you think about it:
http://www.codeproject.com/Articles/802385/A-WPF-MVVM-In-Place-Edit-TextBox-Control

Set a border around a StackPanel.

Here's my XAML code:
<Window x:Class="CarFinder.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Search for cars in TuMomo" Height="480" Width="600">
<DockPanel Margin="8">
<Border CornerRadius="6"
BorderBrush="Gray"
Background="LightGray"
BorderThickness="2"
Padding="8">
<StackPanel Orientation="Horizontal"
DockPanel.Dock="Top"
Height="25">
<TextBlock FontSize="14" Padding="0 0 8 0">
Search:
</TextBlock>
<TextBox x:Name="txtSearchTerm" Width="400" />
<Image Source="/CarFinder;component/Images/Chrysanthemum.jpg" />
</StackPanel>
</Border>
<StackPanel Orientation="Horizontal"
DockPanel.Dock="Top"
Height="25">
</StackPanel>
</DockPanel>
</Window>
The border is set around the entire window. And also, when I create another StackPanel it's added to the right of my previous StackPanel instead of being added under it. What's the reason for this?
What about this one :
<DockPanel Margin="8">
<Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" DockPanel.Dock="Top">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="14" Padding="0 0 8 0" HorizontalAlignment="Center" VerticalAlignment="Center">Search:</TextBlock>
<TextBox x:Name="txtSearchTerm" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Image Source="lock.png" Width="32" Height="32" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</Border>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" Height="25" />
</DockPanel>
You set DockPanel.Dock="Top" to the StackPanel, but the StackPanel is not a child of the DockPanel... the Border is. Your docking property is being ignored.
If you move DockPanel.Dock="Top" to the Border instead, both of your problems will be fixed :)
May be it will helpful:
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="160" Margin="10,55,0,0" VerticalAlignment="Top" Width="492"/>

Resources