2

我正在使用 WPF opacitymask 来“剪切”动画区域的形状。我注意到默认情况下, opacitymask 仅适用于面板的内容,而不适用于整个面板。

将背景设置为透明是一种可行的解决方案,因为(显然它会强制拉伸不透明蒙版。但是感觉像是一种解决方法,所以我很好奇是否有一种“有意”的方式可以使不透明蒙版拉伸到整个网格。

请参阅下面的示例代码和结果:

<Window x:Class="Sandbox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="400" Width="800">
<Window.Resources>
    <ImageBrush x:Key="SomeAlphaMaskBrush" ImageSource="https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Dog_silhouette.svg/1034px-Dog_silhouette.svg.png"></ImageBrush>
</Window.Resources>
<Grid Name="Container">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid Name="Container01" OpacityMask="{StaticResource SomeAlphaMaskBrush}" Background="Transparent">
        <!-- Container01 has a transparent background and it makes the OpacityMask stretch to the entire Container01 -->
        <Rectangle Margin="70" Fill="Black"/>
    </Grid>
    <Grid Grid.Column="1" Name="Container02" OpacityMask="{StaticResource SomeAlphaMaskBrush}">
        <!-- Container02 has no background, the opacitymask is applied only to its content -->
        <Rectangle Margin="70" Fill="Black"/>
    </Grid>
</Grid>

结果输出在这里输出

4

1 回答 1

0

您可以使用ViewportUnits="Absolute"andVisualBrush属性来设置所需的OpacityMask矩形:

<ImageBrush x:Key="SomeAlphaMaskBrush" ViewportUnits="Absolute" Viewport="0 0 20 20" ImageSource="https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Dog_silhouette.svg/1034px-Dog_silhouette.svg.png"/>
于 2018-06-28T18:57:20.553 回答