16

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

在此处输入图像描述

4

2 回答 2

28

您可以在 XAML 中使用VisualBrush. 您只需为 指定 Data 值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>

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" ... />

引用自MSDN

当您不再需要修改可冻结对象时,冻结它会带来性能优势。如果您要在本例中冻结画笔,图形系统将不再需要监视它的变化。图形系统还可以进行其他优化,因为它知道画笔不会改变。

于 2014-04-07T17:46:31.620 回答
8

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

<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>
于 2016-10-12T08:39:25.160 回答