【问题标题】:wpf grid background with image brush and visual brush combinedwpf网格背景与图像画笔和视觉画笔相结合
【发布时间】:2017-02-09 23:24:23
【问题描述】:

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

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

【问题讨论】:

    标签: c# wpf imagebrush visualbrush


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多