wpf带有图像画笔和视觉画笔组合的网格背景

时间:2017-02-09 15:19:43

标签: c# wpf imagebrush visualbrush

我正在尝试将图像设置为窗口的背景。我想在窗口上应用渐变不透明蒙版,并平铺图像。到目前为止,我可以得到或者,但不是两者。这是我对此的蹩脚尝试:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height = "40*" />
            <RowDefinition Height="133*"/>
        </Grid.RowDefinitions>
        <Grid.Resources>
            <Image x:Key="myImage" Source="/GrimshawRibbon;component/Resources/GeometricBackground.png">
                <Image.OpacityMask>
                    <LinearGradientBrush EndPoint = "0.5,0" StartPoint="0.5,1">
                        <GradientStop Color = "#00000000" Offset="0.6"/>
                        <GradientStop Color = "#FF000000" Offset="1"/>
                    </LinearGradientBrush>
                </Image.OpacityMask>
            </Image>
            <ImageBrush x:Key="imageBrush" ImageSource="/GrimshawRibbon;component/Resources/GeometricBackground.png" TileMode="Tile" ViewportUnits="Absolute" Viewport="0,0,800,800"/>
            <VisualBrush x:Key="myBrush" Visual="{StaticResource myImage}" Stretch="None" TileMode="Tile"/>
        </Grid.Resources>
        <DockPanel LastChildFill = "False" Grid.RowSpan="2" Background="{StaticResource imageBrush}"/>
        <ContentControl x:Name="MainContentControl" Content="{Binding CurrentPageViewModel}" Margin="10" Grid.Row="1"/>
        <Button x:Name="btnCancel" Content="Close" Margin="0,0,90,10" HorizontalAlignment="Right" Width="75" Height="36" VerticalAlignment="Bottom" Command="{Binding CloseWindowCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=win}" Grid.Row="1"/>
        <Button x:Name="button" Content="?" Margin="0,0,170,10" Height="36" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="36" Command="{Binding NavigateToHelpCommand, Mode=OneWay}" Grid.Row="1"/>
        <Label x:Name="label" Content="Some label" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="14"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="10,39,0,0" TextWrapping="Wrap" Text="Some tool description that is not too long. It would be good to keep it under two sentences." VerticalAlignment="Top" Height="36" Width="272"/>
    </Grid>

1 个答案:

答案 0 :(得分:1)

您可以指定DockPanel OpacityMask

<DockPanel Grid.RowSpan="2" Background="{StaticResource imageBrush}">
    <DockPanel.OpacityMask>
        <LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1">
            <GradientStop Color="#00000000" Offset="0.6"/>
            <GradientStop Color="#FF000000" Offset="1"/>
        </LinearGradientBrush>
    </DockPanel.OpacityMask>
</DockPanel>

或者可能只是使用矩形作为背景图像:

<Rectangle Grid.RowSpan="2" Fill="{StaticResource imageBrush}">
    <Rectangle.OpacityMask>
        <LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1">
            <GradientStop Color="#00000000" Offset="0.6"/>
            <GradientStop Color="#FF000000" Offset="1"/>
        </LinearGradientBrush>
    </Rectangle.OpacityMask>
</Rectangle>