WPF:被包含对象的半透明画笔消除的窗口透明度 [英] WPF : Transparency of Window eliminated by semi-transparent brush of a contained object

查看:28
本文介绍了WPF:被包含对象的半透明画笔消除的窗口透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Window,属性 AllowsTransparency 设置为 trueBackground 属性设置为一个半透明的颜色,代码:

I have a Window with the property AllowsTransparency set to true, and with the Background property set to a semi-transparent color, the code :

<Window x:Class="InstantSnip.MainView"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            DataContext="{Binding Main, Source={StaticResource Locator}}" 
            AllowsTransparency="True" 
            WindowStyle="None" 
            ResizeMode="NoResize" 
            Topmost="True" 
            Width="180" Height="80" 
            Background="#7FFFFFFF" >

        <Border x:Name="LayoutRoot" 
                BorderBrush="#99FFFFFF" 
                BorderThickness="1" 
                CornerRadius="5">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="3*"></ColumnDefinition>
                    <ColumnDefinition Width="2*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Button Style="{StaticResource NewSnipButton}" Margin="5,0"/>
                <Button Grid.Column="1" Style="{StaticResource ReTrySnippingButton}" Margin="5,0"/>
                <Button Grid.Column="2" Style="{StaticResource CloseButton}" Margin="5,0"/>
            </Grid>
        </Border>
</Window>

这是它的样子:

现在,我想将 Background="#7FFFFFFFF" 赋予 LayoutRoot 边框而不是 Window,以便颜色可以完全适合并且仅在边框的内部边界,当 Background="#7FFFFFFF" 是 Window 的属性时,情况并非如此,正如您在此处看到的:

Now, I want to give the Background="#7FFFFFFF" to the LayoutRoot Border instead of the Window so that the color can fit exactly and only inside the Border's boder, which is not the case when the Background="#7FFFFFFF" is a property of the Window as you can see here :

因此,没有大的变化,代码将如下所示:

And hence, and with no big changes, the code will simply be as follows :

<Window x:Class="InstantSnip.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding Main, Source={StaticResource Locator}}" 
        AllowsTransparency="True" 
        WindowStyle="None" 
        ResizeMode="NoResize" 
        Topmost="True" 
        Width="180" 
        Height="80">

    <Border x:Name="LayoutRoot" 
            BorderBrush="#99FFFFFF" 
            Background="#7FFFFFFF" 
            BorderThickness="1" 
            CornerRadius="5">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="3*"></ColumnDefinition>
                <ColumnDefinition Width="2*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Style="{StaticResource NewSnipButton}" Margin="5,0"/>
            <Button Grid.Column="1" Style="{StaticResource ReTrySnippingButton}" Margin="5,0"/>
            <Button Grid.Column="2" Style="{StaticResource CloseButton}" Margin="5,0"/>
        </Grid>
    </Border>
</Window>

但我得到的是:

我的第一个问题是,为什么?

My first question is, why ?

我的第二个问题是,你能提出解决这个问题的方法吗.

My second question is, can you suggest a solution for this problem.

推荐答案

在您的 Window 中将 Background 设置为 Transparent 即可解决问题

Set Background to Transparent in your Window that will do the trick

例如

<Window x:Class="InstantSnip.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding Main, Source={StaticResource Locator}}" 
        AllowsTransparency="True" 
        WindowStyle="None" 
        ResizeMode="NoResize" 
        Topmost="True" 
        Width="180" 
        Height="80"
        Background="Transparent" >

结果

我没有你的样式所以按钮显示为普通按钮

I don't have your styles so the button appears normal button

编辑

第一个问题的解释

作为 Window 的默认 Background 通常是从 SystemColors.WindowColor.因此,除非您设置任何完全或部分透明的颜色,否则 AllowsTransparency 将无法自行组合透明度.

As the default Background of a Window is usually White inherited from SystemColors.WindowColor. So unless you set any color which is fully or partially transparent, AllowsTransparency will not be able to compose transparency on it's own.

因为任何子控件首先呈现在窗口上,然后整个窗口都被组合到背景中.所以简而言之,无论您在子控件上设置什么透明颜色,它都会在白色背景上组合,导致不透明.

as any child control is rendered on window first then the whole window is composed to the background. So in short no matter what transparent color you set on your child controls it will be composed on a white background leading to no transparency.

因此在Window上设置Transparent,然后您可以选择具有所需透明颜色的圆角边框等.

so set Transparent on Window and then you can choose to have rounded corner border etc with desired transparent color.

来自: WPF 中的透明窗口

WPF 显然可以在其自己的窗口内渲染透明元素,但它也支持以每像素透明度渲染整个窗口.此功能存在一些问题.

WPF can obviously render transparent elements within its own window, but it also supports rendering the entire window with per-pixel transparency. This feature comes with a few issues.

这篇关于WPF:被包含对象的半透明画笔消除的窗口透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆