WPF UserControl背景透明度不起作用

时间:2015-03-04 04:49:15

标签: wpf user-controls transparency

我尝试使用自己的类ModalDialogManager弹出一个usercontrol:Control但背景看起来并不透明,如http://prntscr.com/6cgyc2所示 在我的ModalDialogManager类中,我确实指定了允许透明度的窗口:

        Window w = new Window();
        m_window = w;
        w.Closing += w_Closing;
        w.Owner = GetParentWindow(this);
        w.DataContext = this.DataContext;
        w.SetBinding(Window.ContentProperty, "");
        w.Title = Title;
        w.Icon = Icon;
        w.Height = DialogHeight;
        w.Width = DialogWidth;
        w.ResizeMode = DialogResizeMode;
        // SHOULD IT WORK?!
        w.AllowsTransparency = true;

        double t = GetParentWindow(this).Left;
        if (IsBorderless)
        {
            w.WindowStyle = WindowStyle.None;
            w.ShowInTaskbar = false;

            if (IsStartUpLocationCenter)
            {
                w.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            }
            else
            {
                w.Left = LeftPosition;
                w.Top = RightPosition;
                w.WindowStartupLocation = WindowStartupLocation.Manual;
            }
        }
        else
        {
            w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
        }
        if (IsModal)
            w.ShowDialog();
        else
            w.Show();

但UserControl设计显示正常:http://prntscr.com/6cgyz8

因此,我尝试使用一个窗口来附加usercontrol并使用usercontrol执行window.showDialog(),如下所示:

                Window w = new Window();
                SolidColorBrush b = new SolidColorBrush();
                b.Color = .Colors.Transparent;
                w.Background = b;
                Grid g = new Grid();
                g.Children.Add(new ucSelectCloth());
                w.Content = g;
                g.Background = b;
                w.Height = 300;
                w.Width = 600;
                w.ShowDialog();

正如您所看到的那样,http://prntscr.com/6cgzdk窗口看起来也不透明。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在将Window.AllowsTransparency属性设置为true之前,

窗口仍然是不透明的。将Window.AllowsTransparency设置为true后,您必须将WindowStyle设置为无。

在代码中添加以下行

w.WindowStyle = System.Windows.WindowStyle.None;
w.AllowsTransparency = true;