TileBrush 类
This page is specific to:.NET Framework Version:
.NET Framework 类库
TileBrush 类

更新:2007 年 11 月

描述使用一个或多个图块绘制区域的方法。

命名空间:  System.Windows.Media
程序集:  PresentationCore(在 PresentationCore.dll 中)

语法

public abstract class TileBrush extends Brush
该类为抽象类;有关 XAML 中可用的派生的非抽象类,请参见 继承层次结构。
备注

TileBrush 的派生类定义用于绘制区域的图块的内容。例如,可以通过 ImageBrush 类并使用图像来绘制区域。

使用 TileBrush 可控制绘制区域的方式。例如,Windows Presentation Foundation (WPF) 提供了若干种类型的画笔:

当您使用 TileBrush 绘制区域,而非使用单个拉伸图像来绘制区域时,可以通过使用创建图案的一系列图像图块来绘制区域。

当您使用 TileBrush 绘制某个区域时,需要使用三个组件:内容、图块和输出区域。下面的插图显示了这三个 TileBrush 组件之间的关系。

具有单个图块的 TileBrush 的组件

TileBrush 组件
具有 TileMode 图块的 TileBrush 的组件

平铺 TileBrush 的组成部分

内容TileBrush 可以有不同类型的内容:

可以使用 Viewbox 属性指定 TileBrush 内容的位置和尺寸。

图块TileBrush 可生成一个或多个图块。默认情况下,画笔的内容会拉伸以填充单个图块,该图块也会拉伸以填充输出区域。Viewport 属性用于为 TileBrush 指定基本图块的大小和位置。ViewportUnits 属性决定 Viewport 的大小和位置是否是相对于输出区域(默认行为)而言,或者其大小和位置是否为绝对值。

输出区域:输出区域是画笔绘制的区域,例如 EllipseFillButtonBackground

有关 TileBrush 类的更多信息,请参见使用图像、绘图和 Visual 进行绘制

Freezable 功能

TileBrush 是一种 Freezable 类型。有关 Freezable 功能(例如冻结和克隆)的信息,请参见 Freezable 对象概述

对继承者的说明:

当从 TileBrush 类继承时,必须重写 CreateInstanceCore 方法。有关从 Freezable 类型继承的更多信息,请参见 Freezable 对象概述

示例

本示例演示如何使用 TileBrushTileMode 属性创建图案。

通过 TileMode 属性可以指定如何重复 TileBrush 的内容,即,如何平铺图块内容以填充输出区域。若要创建图案,请将 TileMode 设置为 TileFlipXFlipYFlipXY。还必须设置 TileBrushViewport,使其小于您正在绘制的区域;否则,无论您使用哪个 TileMode 设置,都只能生成一个图块。

下面的示例创建五个 DrawingBrush 对象,每个对象具有不同的 TileMode 设置,并使用它们绘制五个矩形。尽管此示例使用 DrawingBrush 类来演示 TileMode 的行为,但是 TileMode 属性对于所有 TileBrush 对象(即 ImageBrushVisualBrushDrawingBrush)的工作方式都相同。

下面的插图显示了此示例生成的输出。

使用 TileMode 属性创建的图块图案

TileBrush 平铺示例输出
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BrushesIntroduction
{
    public class TileModeExample : Page
    {

        public TileModeExample()
        {
            Background = Brushes.White;
            Margin = new Thickness(20);
            StackPanel mainPanel = new StackPanel();
            mainPanel.HorizontalAlignment = HorizontalAlignment.Left;

            //
            // Create a Drawing. This will be the DrawingBrushes' content.
            //
            PolyLineSegment triangleLinesSegment = new PolyLineSegment();
            triangleLinesSegment.Points.Add(new Point(50, 0));
            triangleLinesSegment.Points.Add(new Point(0, 50));
            PathFigure triangleFigure = new PathFigure();
            triangleFigure.IsClosed = true;
            triangleFigure.StartPoint = new Point(0, 0);
            triangleFigure.Segments.Add(triangleLinesSegment);
            PathGeometry triangleGeometry = new PathGeometry();
            triangleGeometry.Figures.Add(triangleFigure);

            GeometryDrawing triangleDrawing = new GeometryDrawing();
            triangleDrawing.Geometry = triangleGeometry;
            triangleDrawing.Brush = new SolidColorBrush(Color.FromArgb(255, 204, 204, 255));
            Pen trianglePen = new Pen(Brushes.Black, 2);
            triangleDrawing.Pen = trianglePen;
            trianglePen.MiterLimit = 0;
            triangleDrawing.Freeze();

            //
            // Create the first TileBrush. Its content is not tiled.
            //
            DrawingBrush tileBrushWithoutTiling = new DrawingBrush();
            tileBrushWithoutTiling.Drawing = triangleDrawing;
            tileBrushWithoutTiling.TileMode = TileMode.None;

            // Create a rectangle and paint it with the DrawingBrush.
            Rectangle noTileExampleRectangle = createExampleRectangle();
            noTileExampleRectangle.Fill = tileBrushWithoutTiling;

            // Add a header and the rectangle to the main panel.
            mainPanel.Children.Add(createExampleHeader("None"));
            mainPanel.Children.Add(noTileExampleRectangle);

            //
            // Create another TileBrush, this time with tiling.
            //
            DrawingBrush tileBrushWithTiling = new DrawingBrush();
            tileBrushWithTiling.Drawing = triangleDrawing;
            tileBrushWithTiling.TileMode = TileMode.Tile;

            // Specify the brush's Viewport. Otherwise,
            // a single tile will be produced that fills
            // the entire output area and its TileMode will
            // have no effect.
            // This setting uses realtive values to
            // creates four tiles. 
            tileBrushWithTiling.Viewport = new Rect(0, 0, 0.5, 0.5);

            // Create a rectangle and paint it with the DrawingBrush.
            Rectangle tilingExampleRectangle = createExampleRectangle();
            tilingExampleRectangle.Fill = tileBrushWithTiling;
            mainPanel.Children.Add(createExampleHeader("Tile"));
            mainPanel.Children.Add(tilingExampleRectangle);

            //
            // Create a TileBrush with FlipX tiling.
            // The brush's content is flipped horizontally as it is
            // tiled in this example
            //
            DrawingBrush tileBrushWithFlipXTiling = new DrawingBrush();
            tileBrushWithFlipXTiling.Drawing = triangleDrawing;
            tileBrushWithFlipXTiling.TileMode = TileMode.FlipX;

            // Specify the brush's Viewport.
            tileBrushWithFlipXTiling.Viewport = new Rect(0, 0, 0.5, 0.5);

            // Create a rectangle and paint it with the DrawingBrush.
            Rectangle flipXTilingExampleRectangle = createExampleRectangle();
            flipXTilingExampleRectangle.Fill = tileBrushWithFlipXTiling;
            mainPanel.Children.Add(createExampleHeader("FlipX"));
            mainPanel.Children.Add(flipXTilingExampleRectangle);

            //
            // Create a TileBrush with FlipY tiling.
            // The brush's content is flipped vertically as it is
            // tiled in this example
            //
            DrawingBrush tileBrushWithFlipYTiling = new DrawingBrush();
            tileBrushWithFlipYTiling.Drawing = triangleDrawing;
            tileBrushWithFlipYTiling.TileMode = TileMode.FlipX;

            // Specify the brush's Viewport.
            tileBrushWithFlipYTiling.Viewport = new Rect(0, 0, 0.5, 0.5);

            // Create a rectangle and paint it with the DrawingBrush.
            Rectangle flipYTilingExampleRectangle = createExampleRectangle();
            flipYTilingExampleRectangle.Fill = tileBrushWithFlipYTiling;
            mainPanel.Children.Add(createExampleHeader("FlipY"));
            mainPanel.Children.Add(flipYTilingExampleRectangle);

            //
            // Create a TileBrush with FlipXY tiling.
            // The brush's content is flipped vertically as it is
            // tiled in this example
            //
            DrawingBrush tileBrushWithFlipXYTiling = new DrawingBrush();
            tileBrushWithFlipXYTiling.Drawing = triangleDrawing;
            tileBrushWithFlipXYTiling.TileMode = TileMode.FlipXY;

            // Specify the brush's Viewport.
            tileBrushWithFlipXYTiling.Viewport = new Rect(0, 0, 0.5, 0.5);

            // Create a rectangle and paint it with the DrawingBrush.
            Rectangle flipXYTilingExampleRectangle = createExampleRectangle();
            flipXYTilingExampleRectangle.Fill = tileBrushWithFlipXYTiling;
            mainPanel.Children.Add(createExampleHeader("FlipXY"));
            mainPanel.Children.Add(flipXYTilingExampleRectangle);

            Content = mainPanel;
        }

        //
        // Helper method that creates rectangle elements.
        //
        private static Rectangle createExampleRectangle()
        {
            Rectangle exampleRectangle = new Rectangle();
            exampleRectangle.Width = 50;
            exampleRectangle.Height = 50;
            exampleRectangle.Stroke = Brushes.Black;
            exampleRectangle.StrokeThickness = 1;
            return exampleRectangle;
        }

        //
        // Helper method that creates headers for the examples. 
        //
        private static TextBlock createExampleHeader(String text)
        {
            TextBlock header = new TextBlock();
            header.Margin = new Thickness(0, 10, 0, 0);
            header.Text = text;
            return header;
        }
    }
}


<!-- Demonstrates TileMode values. -->
<Page  
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="PresentationOptions"
  Background="White" Margin="20">
  <Page.Resources>

    <!-- Define a Drawing as a resource that it can be easily used
         as content for all the DrawingBrush objects in this example. -->
    <GeometryDrawing x:Key="TriangleDrawing"
      Geometry="M0,0 L50,0 0,50Z" Brush="#CCCCFF"
      PresentationOptions:Freeze="True" >
      <GeometryDrawing.Pen>
        <Pen Thickness="2" Brush="Black" MiterLimit="0" />
      </GeometryDrawing.Pen>
    </GeometryDrawing>
  </Page.Resources>

  <StackPanel HorizontalAlignment="Left">
    <TextBlock Margin="0,10,0,0">None</TextBlock>
    <Rectangle Width="50" Height="50" Stroke="Black" StrokeThickness="1">
      <Rectangle.Fill>

        <!-- The DrawingBrush's content is not tiled in this example. -->
        <DrawingBrush TileMode="None" 
          Drawing="{StaticResource TriangleDrawing}" />
      </Rectangle.Fill>
    </Rectangle>

    <TextBlock Margin="0,10,0,0">Tile</TextBlock>
    <Rectangle Width="50" Height="50" Stroke="Black" StrokeThickness="1">
      <Rectangle.Fill>

        <!-- The DrawingBrush's content is tiled in this example. 
             The Viewport property is set to create four tiles. -->
        <DrawingBrush TileMode="Tile" Viewport="0,0,0.5,0.5" 
          Drawing="{StaticResource TriangleDrawing}"/>
      </Rectangle.Fill>
    </Rectangle>

    <TextBlock Margin="0,10,0,0">FlipX</TextBlock>
    <Rectangle Width="50" Height="50" Stroke="Black" StrokeThickness="1">
      <Rectangle.Fill>

        <!-- The DrawingBrush's content is flipped horizontally as it is
             tiled in this example. -->
        <DrawingBrush 
          TileMode="FlipX" Viewport="0,0,0.5,0.5"
          Drawing="{StaticResource TriangleDrawing}" />
      </Rectangle.Fill>
    </Rectangle>

    <TextBlock Margin="0,10,0,0">FlipY</TextBlock>
    <Rectangle Width="50" Height="50" Stroke="Black" StrokeThickness="1">
      <Rectangle.Fill>

        <!-- The DrawingBrush's content is flipped vertically as it is
             tiled in this example. -->
        <DrawingBrush TileMode="FlipY" Viewport="0,0,0.5,0.5" 
          Drawing="{StaticResource TriangleDrawing}" />
      </Rectangle.Fill>
    </Rectangle>

    <TextBlock Margin="0,10,0,0">FlipXY</TextBlock>
    <Rectangle Width="50" Height="50" Stroke="Black" StrokeThickness="1">
      <Rectangle.Fill>

        <!-- The DrawingBrush's content is flipped horizontally
             and vertically as it is tiled in this example. -->
        <DrawingBrush TileMode="FlipXY" Viewport="0,0,0.5,0.5" 
          Drawing="{StaticResource TriangleDrawing}" />
      </Rectangle.Fill>
    </Rectangle>

  </StackPanel>
</Page>

更多代码

如何:使用图像绘制区域此示例演示如何使用 ImageBrush 类来绘制带有图像的区域。ImageBrush 显示由其 ImageSource 属性指定的单个图像。
如何:保持背景图像的长宽比下面的示例演示如何使用 ImageBrushStretch 属性来保持图像的纵横比。
继承层次结构

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows..::.Freezable
        System.Windows.Media.Animation..::.Animatable
          System.Windows.Media..::.Brush
            System.Windows.Media..::.TileBrush
              System.Windows.Media..::.DrawingBrush
              System.Windows.Media..::.ImageBrush
              System.Windows.Media..::.VisualBrush
线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
平台

Windows Vista

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

版本信息

.NET Framework

受以下版本支持:3.5、3.0
另请参见

参考

其他资源

© 2010 Microsoft Corporation 版权所有。   保留所有权利 | 商标 | 隐私权声明
Page view tracker
为轻量型库评级
x
依无脚本原则生成的轻量型库 (loband),添加了大家要求的功能:搜索框和默认代码语言选择。
您喜欢这个搜索框吗?
您喜欢标签式代码块吗?
此主题有用吗?
提供详细反馈。
谢谢
x
感谢您帮助改善 MSDN Online。
反馈意见
切换视图
经典视图
轻量型视图
无脚本视图
切换视图