WPF Popup UI showing black
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.