我能够在WPF中创建条纹图案,但是如何在XAML中创建这样的图案?WPF中是否有类似的默认画刷?
VisualBrush
在 XAML 中完成。你只需要为 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>
输出
Image
转换为矢量图形(Path),请使用免费且非常有用的Inkscape
。有关更多信息,请参见此链接:
编辑
PresentationOptions
的帮助来Freeze()
您的Brushes,就像这样:<Window x:Class="MyNamespace.MainWindow"
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" ...>
<VisualBrush x:Key="MyVisualBrush" PresentationOptions:Freeze="True" ... />
来自MSDN
的引用:
当你不再需要修改一个可冻结对象时,将其冻结可以提供性能上的好处。如果在这个示例中冻结了画刷,则图形系统将不再需要监视它是否发生变化。图形系统还可以进行其他优化,因为它知道画刷不会改变。
这里是另一种用不同方式进行填充的方法:
<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>
Data="M 0 5 L 5 0"
就足以生成上述图案。 - maxp
PresentationOptions
来冻结您的VisualBrush
,例如:PresentationOptions:Freeze="True"
。有关更多信息,请参见此链接
。 - Anatoliy Nikolaev