在WPF中创建填充图案

Vah*_*hid 14 wpf xaml hatchstyle

我能够在WPF中创建条纹模式,但是如何在XAML中创建这样的模式?在WPF中是否有默认类似的刷子?

在此输入图像描述

Ana*_*aev 24

你可以在XAML中使用VisualBrush.您只需要为其指定数据值Path,例如:

XAML

<Window.Resources>
    <VisualBrush x:Key="MyVisualBrush" TileMode="Tile" Viewport="0,0,15,15" ViewportUnits="Absolute" Viewbox="0,0,15,15" ViewboxUnits="Absolute">
        <VisualBrush.Visual>
            <Grid Background="Black">
                <Path Data="M 0 15 L 15 0" Stroke="Gray" />
                <Path Data="M 0 0 L 15 15" Stroke="Gray" />
            </Grid>
        </VisualBrush.Visual>
    </VisualBrush>
</Window.Resources>

<Grid Background="{StaticResource MyVisualBrush}">
    <Label Content="TEST" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

Output

在此输入图像描述

对于转换Image为矢量图形(路径)使用Inkscape,这是免费的,非常有用.有关更多信息,请参阅此链接:

Vectorize Bitmaps to XAML using Potrace and Inkscape

Edit

为了获得更好的性能,你可以Freeze()借助于PresentationOptions这样的帮助:

<Window x:Class="MyNamespace.MainWindow"
        xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" ...>

    <VisualBrush x:Key="MyVisualBrush" PresentationOptions:Freeze="True" ... />
Run Code Online (Sandbox Code Playgroud)

引用自MSDN:

当您不再需要修改freezable时,冻结它可以提供性能优势.如果您在此示例中冻结画笔,则图形系统将不再需要监视它以进行更改.图形系统也可以进行其他优化,因为它知道画笔不会改变.


Dre*_*kes 7

这是另一种方法,适用于不同的孵化方式:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
  Background="Black">

  <Page.Resources>

    <VisualBrush x:Key="HatchBrush" TileMode="Tile"
                 Viewport="0,0,5,5" ViewportUnits="Absolute"
                 Viewbox="0,0,5,5" ViewboxUnits="Absolute"
                 po:Freeze="True">
      <VisualBrush.Visual>
        <Path Data="M 0 5 L 5 0 M -2 2 L 2 -2 M 3 7 L 7 3"
              Stroke="#80ffffff" StrokeEndLineCap="Square"
              RenderOptions.EdgeMode="Aliased" />
      </VisualBrush.Visual>
    </VisualBrush>

  </Page.Resources>

  <Grid Background="{StaticResource HatchBrush}" />

</Page>
Run Code Online (Sandbox Code Playgroud)