C# WPF弹出窗口具有黑色边框,而不是透明边框
我正在以编程方式为WPF窗口中的某个元素创建弹出窗口,但无法摆脱黑色边框:C# WPF弹出窗口具有黑色边框,而不是透明边框,c#,wpf,xaml,.net-4.0,popup,C#,Wpf,Xaml,.net 4.0,Popup,我正在以编程方式为WPF窗口中的某个元素创建弹出窗口,但无法摆脱黑色边框: var p = new Popup { PlacementTarget = target, IsOpen = true, StaysOpen = false, AllowsTransparency = true }; // Add the popup content p.Child = new Views.MapLocationInformation {DataContext =
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>
<UserControl ...
mc:Ignorable="d"
Background="{x:Null}"
AllowsTransparency="True"
...
我找不到边界、背景填充和透明度设置的任何组合,这将使黑色区域透明。有什么想法吗?您的弹出窗口允许透明,但不使用透明背景。改为:
var p = new Popup {
PlacementTarget = target,
IsOpen = true,
StaysOpen = false,
AllowsTransparency = true,
Background = Brushes.Transparent
};
这应该能奏效。另外,黑色位在右边和底部更宽的原因是由于
边框上的边距,这实际上是无用的。我建议您也删除它。这是由MapLocationInformation
的Background
属性引起的。只需将UserControl
的Background
设置为null,并将allowtransparency
设置为True
即可解决此问题,如下所示:
<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>
<UserControl ...
mc:Ignorable="d"
Background="{x:Null}"
AllowsTransparency="True"
我也遇到了同样的问题。问题似乎是,当弹出窗口的IsOpen
属性过早设置为True
时,透明度无法正常工作
我的解决方案是将设置IsOpen
从构造移动到弹出窗口的Loaded
事件
myPopup.Loaded += (sender, args) => { myPopup.IsOpen = true; };
您确定边框不属于您的MapLocationInformation控件吗?当您将一个红色矩形作为子对象时会发生什么情况?我已经添加了用户控件定义,并将处理其他内容。感谢您的输入。wpf中没有任何用于弹出的背景属性。