【问题标题】:WPF : Transparency of Window eliminated by semi-transparent brush of a contained objectWPF:窗口的透明度被包含对象的半透明画笔消除
【发布时间】:2014-07-17 03:29:02
【问题描述】:

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

<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="#7FFFFFFF" 提供给 LayoutRoot 边框而不是窗口,以便颜色可以完全适合并且仅在边框的边框内,当 Background="#7FFFFFFF" 是属性时,情况并非如此正如您在此处看到的那样:

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

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

但我得到的是这个:

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

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

【问题讨论】:

    标签: c# .net wpf windows xaml


    【解决方案1】:

    在您的Window 中将Background 设置为Transparent 就可以了

    例如

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

    结果

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

    编辑

    第一个问题的解释

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

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

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

    发件人: Transparent Windows in WPF

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

    【讨论】:

    • 完美,感谢您的错字和解释,我现在已经全部工作并理解了。
    【解决方案2】:

    如果我正确阅读了您的代码,您将 Background 属性从窗口移到了边框控件。我的猜测,也只是猜测,是 UserControls 不支持透明背景。

    这听起来类似于AllowTransparency alternative for rounded edge forms。也许它接受的答案对你有用。

    【讨论】:

    • 我的代码中没有UserControl,有窗口和Border、Grid和按钮,都支持透明,你指的是什么,我还是看你建议的链接跨度>
    • 哦,对不起,我以为 Border 是一个用户控件。也许它不支持使用 alpha 通道的透明度。我认为它具有不透明度属性。将其设置为 0.5 是否可以满足您的需求?
    猜你喜欢
    • 2020-08-18
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2010-11-20
    • 2021-11-01
    相关资源
    最近更新 更多