简体   繁体   English

WPF-如何使窗口不透明为白色

[英]WPF - How to make Window opacity to white

I want to make the window have the effect can change the whole window which has many elements evenly to white, as the window behind in the picture: 我要使窗口具有效果,可以将具有许多元素的整个窗口均匀地更改为白色,如图中的窗口所示:

后面的窗户变成白色

I use code like 我使用类似的代码

    public MainWindow()
    {
        this.Opacity = 0.5;
    }

but it change to black 但变成黑色

在此处输入图片说明

How to make it whole evenly change to white even when there're many Element in the Window and don't set the window Style to none?(Because set Window AllowTransparent seems have to set the Style to none at the same time) 即使窗口中有很多元素并且不将窗口样式设置为空,如何使整个颜色均匀地变为白色?(因为设置窗口AllowTransparent似乎必须同时将样式设置为无)

I hope can using code to do it, because I want to do it dynamically. 我希望可以使用代码来做到这一点,因为我想动态地做到这一点。

(Or possibly it use UserControl but not Window to achieve this effect? maybe the UserControl use with the Window and set the UserControl to Transparent can do it (或者可能使用UserControl而不使用Window来达到此效果?也许UserControl与Window一起使用并将UserControl设置为Transparent可以做到这一点。

----After I try, I find UserControl doesn't have property AllowTransparent, so it seems imposible use this way ) ----尝试后,我发现UserControl没有属性AllowTransparent,因此使用这种方法似乎是不可能的)

如果只需要淡化工作区,则可以放置叠加层-对窗口上所有内容进行一些空的半透明控件。

Basically, you have two options: 基本上,您有两种选择:

Use white Background color on Window and change Opacity on the window children, so the white starts to shine through 在“窗口”上使用白色Background色,并更改窗口子项的“ Opacity ”,因此白色开始发光

<Window Background="White">
    <Grid Opacity="{Binding WhiteOutVisibility}" Background="WhiteSmoke">
        <YourContent/>
    </Grid>
</Window>

Use a white overlay control with alpha or Opacity that lets the actual content shine through 使用具有Alpha或Opacity的白色覆盖控件,使实际内容发光

<Grid>
    <YourContent/>
    <Border Background="#80ffffff" Visibility="{Binding WhiteOutVisibility}"/>
</Grid>

In my opinion, you should use a white overlay if you want to block user interaction with the window content and white background if you want to continue user interaction. 我认为,如果要阻止用户与窗口内容进行交互,则应使用白色叠加层;如果要继续与用户交互,则应使用白色背景。

You can achieve this effect by laying a canvas over your window, and setting the background to white and an opacity value. 您可以通过在窗口上方放置画布并将背景设置为白色和不透明度值来实现此效果。 Some xaml like this will work. 像这样的一些xaml将起作用。 Just change the UserControl for Window. 只需更改UserControl for Window。

<UserControl x:Class="View.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="40" d:DesignWidth="100">
    <Grid>
        <TextBox  Text="Hello there" />
        <!-- this will show faintly -->
        <Canvas Background="White" Opacity="0.8"></Canvas>
    </Grid>
</UserControl>

This xaml looks like this: 这个xaml看起来像这样:

在此处输入图片说明

The Window type has property AllowsTransparency . 窗口类型具有属性AllowsTransparency You can find it property on your window properties in MSVisualStudio. 您可以在MSVisualStudio的窗口属性中找到它属性。 This can solve your problem. 这样可以解决您的问题。

Thanks for Phillip Ngan and grek40 s' answer, 感谢Phillip Ngan和grek40的回答,

both Grid and Canvas with background white and opacity works, I write some test code that can show the effect 具有背景白色和不透明度的GridCanvas均可工作,我编写了一些可以显示效果的测试代码

Xaml Part Xaml部分

<Window x:Class="WPFAbitraryTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Button  Grid.Row="0" Background="Blue" Foreground="White" FontSize="20" Click="SwitchOpacity_OnClick">Clcik to SwitchOpacity</Button>
        <Button Grid.Row="1" Background="ForestGreen">hi2</Button>
        <ListBox Grid.Row="2" Background="Orange">
            <ListBoxItem>ListBox Item #1</ListBoxItem>
            <ListBoxItem>ListBox Item #2</ListBoxItem>
            <ListBoxItem>ListBox Item #3</ListBoxItem>
        </ListBox>

        <!-- <Grid Grid.Row="1" Grid.RowSpan="2" Opacity="0.9" Background="WhiteSmoke"/> -->


        <Canvas Name="WhiteMaskCanvas"  Grid.Row="1" Grid.RowSpan="2" Background="White" Opacity="0.5"></Canvas>

    </Grid>
</Window>

.

Class Part 类部分

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void SwitchOpacity_OnClick(object sender, RoutedEventArgs e)
        {

            int opacityVal = 0;

            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i <= 1000; i++)
                {
                    int j = 0;
                    Thread.Sleep(100);


                    //Use ++ % to change Opacity
                    this.Dispatcher.Invoke(
                        DispatcherPriority.SystemIdle,
                        new Action(() =>
                        {
                            WhiteMaskCanvas.Opacity = ++opacityVal % 10 / 10.0;
                        }));


                    ////Use Abs Cosine to Change Opacity 
                    //this.Dispatcher.Invoke(
                    //    DispatcherPriority.SystemIdle,
                    //    new Action(() =>
                    //    {
                    //        WhiteMaskCanvas.Opacity = 
                    //            Math.Abs(Math.Sin(++opacityVal*0.1)) ;
                    //    }));

                }
            });

        }

    }

.

The Result: 结果:

在此处输入图片说明

.

further code, 进一步的代码,

if want to make the canvas mask whole window, you can change the canvas to 如果要使画布蒙版成为整个窗口,可以将画布更改为

<Canvas Name="WhiteMaskCanvas"  Grid.Row="0" Grid.RowSpan="3" Background="White" Opacity="0.5"></Canvas>

and add code to class: 并将代码添加到类:

public MainWindow()
        {
            InitializeComponent();
            WhiteMaskCanvas.Visibility = Visibility.Collapsed;
        }


private void SwitchOpacity_OnClick(object sender, RoutedEventArgs e)
        {
            WhiteMaskCanvas.Visibility = Visibility.Visible;

            int opacityVal = 0;

            Task.Factory.StartNew(() =>
            {
            //below same as code above

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM