wpf grid background with image brush and visual brush 组合

wpf grid background with image brush and visual brush combined

我正在尝试将图像设置为 window 的背景。我想在 window 上应用渐变不透明蒙版并平铺图像。到目前为止,我可以得到其中之一,但不能同时得到。这是我对此的蹩脚尝试:

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

您可以分配 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>