WPF Popup UI showing black - wpf

I am using a WPF Popup control, and it is showing the background as black. I put a StackPanel inside it with Background="Transparent", but that does not help.
<Popup PlacementTarget="{Binding ElementName=parentStackPanel}" Placement="Center"
IsOpen="False" Name="m_popWaitNotifier" PopupAnimation="None"
AllowsTransparency="False">
<StackPanel Orientation="Vertical" Background="Transparent">
<uc:CircularProgressBar x:Name="CB" StartupDelay="0"
RotationsPerMinute="20"
Height="25" Foreground="White"
Margin="12"/>
</StackPanel>
</Popup>
How does one make the background on Popup transparent (or any color)?

You need to set the AllowsTransparency="True" Popup Property to True
Here is an example:
<Grid>
<StackPanel>
<Button Click="Button_Click" Width="100" Height="20" Content="Click" />
<Popup x:Name="popup" Width="100" Height="100" AllowsTransparency="True">
<Grid Background="Transparent">
<TextBlock Text="Some Text" />
</Grid>
</Popup>
</StackPanel>
</Grid>
and the click handler
private void Button_Click(object sender, RoutedEventArgs e)
{
popup.Visibility = System.Windows.Visibility.Visible;
popup.IsOpen = true;
}

The base color of a Popup, or a Window for that matter, is black. You rarely see it for a Window because Window has a Background property and it defaults to a solid color, but if you set Window.Background to Transparent it will also be black. But Popup doesn't have a Background property and so, pardon the pun, this problem "pops up".
If you want the Popup to be transparent, you need to set AllowsTransparency="True". However, if you want the Popup to be a solid color, the simplest approach is to make the child of the Popup a Panel that supports the Background property and set that property to the color you desire and then set the child of the Panel to be the content you intended for the Popup in the first place. I suggest Grid as it won't affect the layout of your Popup. It's only effect will be to give you the background color you desire.

Make sure that the allow transparency is set to true, vertical and horizontal alignments are centered, and the height and width are set to Auto.
For example:
<Popup Name="popup1" Placement="Top" PlacementTarget="{Binding ElementName=button2}" AllowsTransparency="True" Height="Auto" Width="Auto" Panel.ZIndex="1" HorizontalOffset="-5" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel Height="92" HorizontalAlignment="Left" Margin="93,522,0,0" Name="stackPanelPop" VerticalAlignment="Top" Width="147">
</StackPanel>
</Popup>

Another possible cause:
using IsOpen="True" in markup before AllowTransparency="True"
Switching the order fixes it.

My guess is that the CircularProgressBar is actually causing the Black background. The only other way that this could happen is if there was a Style or something set on one of the controls (Popup or StackPanel or...).
Here is a quick-n-dirty example that shows a TextBlock in a popup when a checkbox is checked. The colors chosen are just to make sure things stand out visually:
<StackPanel x:Name="stackPanelLayout">
<StackPanel.Background>
<RadialGradientBrush Center="0.75, 0.75"
SpreadMethod="Reflect">
<GradientStop Color="LightBlue" Offset="0" />
<GradientStop Color="SeaGreen" Offset="0.5" />
<GradientStop Color="MidnightBlue" Offset="0.75" />
</RadialGradientBrush>
</StackPanel.Background>
<CheckBox x:Name="chkShowPopup"
FontSize="20"
Foreground="White"
Content="Show Popup" />
<Popup PlacementTarget="{Binding ElementName=stackPanelLayout}"
Placement="Center"
IsOpen="{Binding ElementName=chkShowPopup, Path=IsChecked}"
Name="m_popWaitNotifier"
PopupAnimation="Slide"
AllowsTransparency="True">
<StackPanel Orientation="Vertical" Background="Transparent">
<TextBlock Foreground="White" FontSize="30" FontWeight="Bold" Text="PopUp" />
</StackPanel>
</Popup>
</StackPanel>
So, two tests you can do to determine what is happening:
Replace the CircularProgressBar with a simple TextBlock or other control that you don't have a Style applied to.
Put the CircularProgressBar as a standalone control somewhere on your window, or on an otherwise blank test Window.

As per this article Why is my WPF Popup black and how do I get it positioned properly?
:
You need to set the AllowsTransparency property on the Popup to True, and set the PlacementTarget and Placement properties to control the position the Popup opens in.
As per the code in question:
<Popup PlacementTarget="{Binding ElementName=parentStackPanel}" Placement="Center" IsOpen="False" Name="m_popWaitNotifier" PopupAnimation="None" AllowsTransparency="False">
<StackPanel Orientation="Vertical" Background="Transparent">
<uc:CircularProgressBar x:Name="CB" StartupDelay="0" RotationsPerMinute="20" Height="25" Foreground="White" Margin="12"/>
</StackPanel>
</Popup>
the PlacementTarget is set to parentStackPanel, whereas the questioner has mentioned:
Hi Svetlozar: I tried this but it does
not work. For me though I do not have
a StackPanel outside the Popup, but I
have a StackPanel within the Popup
that holds a couple of control on it
The problem could be that Popup could not find the PlacementTarget 'parentStackPanel' because it does not exist.

The problem is that the grid is not orientation places it outside of the popup.
Remove VerticalAlignment and horizontalAlignment from all the controls inside the popup, and it will work correctly

Quite old, but may help someone: Add InitializeComponent(); in the constructor, it solved my problem:
class MyPopupClass : Popup {
/*
...
*/
public MyPopupClass () {
InitializeComponent();
/*
...
*/
}
/*
...
*/
}

Related

Need to make full screen popup in WPF

I want to open full screen popup but what is happening right now is that from the top it increases but bottom transparent or any color effect is not visible.Below is the code i am using .please let me know where i am wrong.Please see popup design CLICK HERE
<Popup StaysOpen="False" x:Name="popintaling" Width="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}" Placement="Center" IsOpen="False" Height="900" Panel.ZIndex="1" HorizontalAlignment="Center" VerticalAlignment="Center" AllowsTransparency="True" PopupAnimation="Fade" ClipToBounds="True" OpacityMask="#FFFFAA00" RenderTransformOrigin="0,0" >
<Popup.Effect>
<DropShadowEffect/>
</Popup.Effect>
<Border Width="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}" Height="900" BorderBrush="#7F1D1A1A" Margin="0,0,0,0" BorderThickness="300,300,300,300" HorizontalAlignment="Center" >
<Canvas Margin="0,100,0,90" Height="800" Width="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}" Panel.ZIndex="999" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FFF3800C" >
//some code here
</Canvas>
</Border>
</Popup>
A Popup that covers more than 75 percent of the screen, reduces its width first and then its height to meet the maximum coverage limit of 75 percent.You cannot cover make it full screen.
Read the documentation : PlacementMode
after InitializeComponent in constructor, set popup child width and height from code
(popintaling.Child as FrameworkElement).Height = Window.Current.Bounds.Height;
(popintaling.Child as FrameworkElement).Width = Window.Current.Bounds.Width;
A full screen popup is not a popup. Just put that code into a border with a Background color and handle the events yourself. Also to make your App fullscreen just set the WindowState to Maximized, and the WindowStyle to None

How to avoid sizing grip overlap in Window

I made a WPF Window containing StatusBar with StatusBarItem and ProgressBar.
The Window has a property ResizeMode set to CanResizeWithGrip.
It shows the sizing grip properly, but it overlaps elements underneath:
How can I avoid this overlap? I can set right margin to the progress bar, but how large? I don't want to use any magic numbers or hardcoded values. It would be also nice to have this resolved purely in XAML.
You can change style for "Window" considering all wishes.
http://msdn.microsoft.com/en-us/library/aa969824(v=vs.110).aspx
or https://stackoverflow.com/a/8278917/3492412
Or can do something like this
<StatusBar VerticalAlignment="Bottom">
<StatusBarItem x:Name="statusbar" Background="Gray" HorizontalContentAlignment="Stretch">
<DockPanel>
<ResizeGrip DockPanel.Dock="Right" Visibility="Hidden" />
<ProgressBar Background="red" Height="20" Value="50" />
</DockPanel>
</StatusBarItem>
</StatusBar>

Setting the background color of a FlowDocument in a FlowDocumentReader

I have been using a RichTextBox to show FlowDocuments in a WPF app, but I found that there is something called FlowDocumentReaderwhich gives me alot of functionality for free. The problem is that while the background in the RichTextBox was shown as white, it is now completely transparent.
I have tried setting the Backgroundproperty, but that only changes the toolbar at the bottom.
<FlowDocumentReader Grid.Row="1" Grid.Column="1" Name="rtbShowDoc" Margin="20, 0" Background="White">
<FlowDocumentReader.Effect>
<DropShadowEffect BlurRadius="10" Color="Black" ShadowDepth="3" />
</FlowDocumentReader.Effect>
</FlowDocumentReader>
I can do an ugly fix with a DockPanel, but that does not seem like the right way to do it.
<DockPanel Grid.Row="1" Grid.Column="1" Margin="20, 0" Background="White">
<DockPanel.Effect>
<DropShadowEffect BlurRadius="10" Color="Black" ShadowDepth="3" />
</DockPanel.Effect>
<FlowDocumentReader Grid.Row="1" Grid.Column="1" Name="rtbShowDoc" Background="White">
</FlowDocumentReader>
</DockPanel>
How can I set the background of a FlowDocumentReader?
EDIT: Added screenshot of running application. As you can see the dropshadow effect is applied to all text inside the FlowDocument.
Try setting the background of the FlowDocument
FlowDocument.Background Property
In C#:
FlowDocument.Background = Brushes.Red;

WPF Popup control on ImageMouseLeftButtonDown disappears immediately

I have a popup defined as follows,
<Popup x:Name="popLines"
Placement="Bottom"
IsOpen="False"
Width="145" Height="42"
StaysOpen="False"
PopupAnimation="Fade"
AllowsTransparency="True"
HorizontalOffset="-2" VerticalOffset="0">
<Grid Margin="2">
<Path StrokeThickness="0.7" StrokeLineJoin="Round" Fill="#FFFFFFFF" Stretch="Fill" Stroke="Black" Data="M6.5,0.5 L30.167,0.5 30.167,8.4999992 190.16701,8.4999992 190.16701,44.166001 0.5,44.166001 0.5,8.4999992 6.5,8.4999992 6.5,0.5 z">
</Path>
<Grid>
<StackPanel Orientation="Horizontal">
<TextBox BorderBrush="Black" BorderThickness="0.5" Margin="5,10,2,2" Width="110" Height="20" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ToolTip="Excel File Path"></TextBox>
<Image Source="/App_Desktop;component/Resources/save.png" Margin="2,10,5,2" Width="16" Height="16"></Image>
</StackPanel>
</Grid>
</Grid>
</Popup>
I set IsOpen=true when an image MouseLeftButtonDown event fires.Except, the popup disappears as soon as it appears. What is going wrong?
I think what happens is, that the the MouseLeftButtonUp event fires while the Mouse is not over the Popup. Try holding the mousebutton down and move your mouse so it is above the popup to see if it stays open, and then release the mousebutton.
you can solve this by setting StaysOpen="True" or by setting IsOpen=true in a click eventhandler or MouseButtonUp eventhandler.
Also using your example with the image, you could have a transparent area right above the image in your popup visualtree that 'captures' the MouseUp event when the popup is open. you would have to change the placement option and bind that area to the image's width and height properties.

WPF ToolTip containing buttons can not recieve Mouse events, alternative?

I want a formatbar like in Office 2010 with WPF:
Select Text and then click buttons on appearing tooltip to execute a command
How can I make that work?
The ToolTip window can't accept focus, use the Popup control instead. It's a bit more cumbersome, than a tooltip, because many useful properties are set to false by default, here is a tiny example:
<Popup x:Name="samplePopup" PopupAnimation="Fade" Placement="Mouse" AllowsTransparency="True" StaysOpen="False" >
<Popup.Child>
<StackPanel Margin="10" >
<TextBlock Text="Some Text" HorizontalAlignment="Center" />
<Button Content="Close" HorizontalAlignment="Center" />
</StackPanel>
</Popup.Child>
</Popup>

Resources