WPF XAML透明效果设置

转自:http://hi.baidu.com/willbelate/item/8296f40b15a7e3354bc4a35e

<Window x:Class="WPF中的三种透明方法.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.Background>
            <ImageBrush
                ImageSource="D:c.png"
                Stretch="Fill"
                ViewportUnits="RelativeToBoundingBox"
                Viewport="0,0,0.1,0.1"
                TileMode="Tile"/>
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Content="Hello" Opacity="0.5"></Button>
        <Button Grid.Row="1" Content="Hello" Background="#90000000"></Button>
        <Button Grid.Row="2" Content="Hello">
            <Button.OpacityMask>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Color="Transparent" Offset="0.3"/>
                    <GradientStop Color="Blue" Offset="1"/>
                </LinearGradientBrush>
            </Button.OpacityMask>
        </Button>
    </Grid>
</Window>
xmal设置

在WPF中可以使用三种方法使元素透明,

第一种方法是设置元素的Opacity属性,如果元素不透明,则Opacity的值为1,如果透明则值为大于0小于1的小数数值。

第二种方法是通过元素的背景颜色,可以使用#90000000的方式设置,也就是Argb的方式设置,#号后面的两位置透明颜色,如果出现#FF000000那么背景色是全透明的。

第三种方法是通过元素的OpacityMask透明掩码属性来设置透明,此属性接收一个画刷对象,可以使用LinearGradientBrush制做线性渐变透明的效果,也可以使用SolidBrush制做普通透明的画刷。

比较快的方法是Opacity="x"(0<x<1)

原文地址:https://www.cnblogs.com/bkycjj/p/3341192.html