2010-10-11 65 views
39

我正在使用WPF Popup控件,它将背景显示为黑色。我把一个StackPanel与Background =“Transparent”放在它里面,但这没有帮助。WPF Popup UI显示黑色

<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> 

有人可以告诉我如何使背景弹出透明(或任何颜色)?

+0

请张贴一些示例XAML。你所描述的不是默认行为,但很难对它做更多的说明,而不知道如何定义它。 – 2010-10-11 15:41:38

+0

@PN另外 - 如果您添加代码,请选择(突出显示),然后选择编辑文本框上方的“代码”按钮。这会将代码格式化为更具可读性。 – 2010-10-11 17:50:11

+0

如果将CircularProgressBar控件直接添加到窗口(作为测试),会发生什么情况。黑色背景能从那里来吗? – 2010-10-11 17:52:04

回答

64

您需要的AllowsTransparency="True"弹出属性设置为true

下面是一个例子:

<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> 

,并单击处理

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    popup.Visibility = System.Windows.Visibility.Visible; 
    popup.IsOpen = true; 
} 
+0

嗨Svetlozar:我试过但它不起作用。对我来说,虽然我没有在Popup之外的StackPanel,但是我在Popup中有一个StackPanel,它拥有对它的几个控制。 – 2010-10-11 16:12:10

+0

这个完美的作品应该被标记为答案。 – 2015-11-30 18:25:31

3

我的猜测是,CircularProgressBar实际上造成黑色的背景。这可能发生的唯一的另一种方式是,如果在某个控件(Popup或StackPanel或...)上有Style或某项设置。

下面是一个快速n-dirty的例子,当复选框被选中时,弹出一个TextBlock。所选择的颜色,只是为了确保事情脱颖而出视觉:

<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> 

所以,两个测试可以做,以确定发生了什么:

  1. 用一个简单的TextBlock或其他控制替换CircularProgressBar那您没有适用于Style的样式。
  2. 将CircularProgressBar作为独立控件放置在窗口或其他空白测试窗口的某处。
0

问题是网格不是方向的,它将它放在弹出窗口之外。 从弹出里面所有的控件删除VerticalAlignment和的Horizo​​ntalAlignment,它会正常工作

1

按这篇文章Why is my WPF Popup black and how do I get it positioned properly?
您需要设置在弹出为True AllowsTransparency属性,并设置PlacementTarget和安置属性来控制弹出在打开位置

按照所讨论的代码:

<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> 

的PlacementTarget设为parentStackPanel,而提问者提到:

嗨Svetlozar:我试过这个,但它确实不起作用 。对我来说,虽然我有 一个StackPanel 弹出,但我 持有一对夫妇控制的上

弹出 内有一个StackPanel的问题可能是弹出可能找不到 PlacementTarget'parentStackPanel',因为它不存在。

10

对于这个问题,PopupWindow的底色是黑色的。你很少看到它的Window,因为Window有一个Background属性,它默认为纯色,但如果你设置Window.BackgroundTransparent它也会是黑色的。但Popup没有Background财产,所以,请原谅双关语,这个问题“弹出”。

如果你想Popup是透明的,你需要设置AllowsTransparency="True"。但是,如果您希望Popup为纯色,最简单的方法是使支持Background属性的Popup a Panel的子项并将该属性设置为所需的颜色,然后将Panel的子项设置为您首先要为Popup准备的内容。我建议Grid,因为它不会影响您的Popup的布局。这只是为了给你你想要的背景颜色。

8

确保允许透明度设置为true,垂直和水平对齐居中,高度和宽度设置为自动。

例如:

<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> 
0

很老了,但是可以帮助别人:在构造函数中添加InitializeComponent();,它解决了我的问题:

class MyPopupClass : Popup { 
    /* 
    ... 
    */ 
    public MyPopupClass() { 
     InitializeComponent(); 
     /* 
     ... 
     */ 
    } 
    /* 
    ... 
    */ 
} 
+0

Downvote ...因为我不喜欢这个答案......只是如此......错了...... – WoIIe 2016-02-01 11:06:09

+0

@WoIIe,感谢您的反馈。我很高兴知道我的错误。它对我有效,所以我错过了什么? – Tar 2016-02-02 10:32:12

+0

我很抱歉写这么粗鲁的评论。我只是不认为你的答案可以帮助OP以任何方式解决他的问题。原因之一是'InitializeComponent()'-Method自动生成到构造函数中,并且完全需要'Control'或'FrameWorkelement'在WPF应用程序中工作。绝对没有理由删除这一行。您还有书写的代码,它显示了“Popup”控件的自定义实现,OP不是这种情况。由于他使用普通的'Popup'类,因此他没有办法忘记他的Popup的'this.InizeizeComponent()'方法。 – WoIIe 2016-02-02 10:54:14

1

另一个可能的原因:

  • 在AllowTransparency =“True”之前在标记中使用IsOpen =“True”

切换订单可以修复它。