WPF弹出窗口的边框是黑色而不是透明的

7
我正在以编程方式为WPF窗口中的元素创建弹出窗口,但无法去除黑色边框:

Popup with border

var p = new Popup {
     PlacementTarget = target,
     IsOpen = true,
     StaysOpen = false,
     AllowsTransparency = true
};

// Add the popup content
p.Child = new Views.MapLocationInformation {DataContext = context};

用户控件MapLocationInformation在XAML中定义为以下形式:
<UserControl ...
 mc:Ignorable="d" 
 Background="Transparent"
 d:DesignHeight="65" d:DesignWidth="401">

 <Border BorderThickness="1"
        CornerRadius="5"
        BorderBrush="{StaticResource ExpanderHeaderBorderGradient}"
        Background="White"
        Margin="0 0 8 8">

        <Stackpanel> ... </Stackpanel>
     </Border>
</UserControl>

我找不到任何边框、背景填充和透明度设置的组合,可以将黑色区域设为透明。有什么想法吗?


1
你确定边框不属于你的MapLocationInformation控件吗?当你将红色矩形作为子元素时会发生什么? - Slugart
我已经添加了用户控件定义,并将尝试使用其他内容。感谢您的建议。 - Christian Studer
5个回答

3

我刚遇到了同样的问题。问题似乎是这样的,当弹出窗口的IsOpen属性太早设置为True时,透明度无法正常工作。

我的解决方案是将设置IsOpen为true的操作从构造函数移动到弹出窗口的Loaded事件中。

myPopup.Loaded += (sender, args) => { myPopup.IsOpen = true; };

2
您的弹出窗口允许透明度,但未使用透明背景。请更改为:
var p = new Popup {
     PlacementTarget = target,
     IsOpen = true,
     StaysOpen = false,
     AllowsTransparency = true,
     Background = Brushes.Transparent
};

这应该可以解决问题。另外,黑色边框右侧和底部宽度更宽的原因是由于您的 Border 上的 Margin,实际上有点无用。建议您也将其删除。


8
WPF中的弹出窗口没有任何背景属性。 - Ashish-BeJovial

1

这是由于MapLocationInformationBackground属性引起的。只需将您的UserControlBackground设置为null,并将AllowsTransparency设置为True,就可以像这样修复它:

<UserControl ...
 mc:Ignorable="d" 
 Background="{x:Null}"
 AllowsTransparency="True"

0
在我的情况下,我只需要添加AllowsTransparency = truePopup中。我有一种感觉Kent's answer是正确的,因为那里包含一个不存在的背景属性并不相关,因为它是不必要的。

0

@NikoR 是正确的。但这只是属性的顺序问题。

您必须在设置 IsOpen 之前设置 AllowsTransparency

var popup = new Popup
{
  AllowsTransparency = true,
  IsOpen = true
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接