在从 WinForm 向 WPF 转型的过程中,很多开发者都会思考如何在 WPF 中实现各种自定义图形的绘制。WinForm 中通常通过 GDI+或者直接在控件的 Paint 事件中手动绘图。而在 WPF 中,使用 Shape 派生类(例如 Rectangle, Ellipse, Line 等)就可以更直观地在界面上绘制图形。本篇文章将介绍如何在 WPF 中使用 Rectangle,并说明与 WinForm 的主要差异,以及如何利用 WPF 的强大样式和动画特性让矩形图形更灵活多变。
OnPaint 或 Paint 事件内通过 Graphics 对象手动调用 DrawRectangle 等方法进行绘制。Invalidate / Refresh)或联动其它 UI 事件。System.Windows.Shapes.Rectangle 控件来绘制矩形。可以把它看作一个 UIElement,天然支持布局系统。Width, Height, Fill, Stroke 等)进行绑定或使用样式,轻松调整或动画化。下面是一个最基础的示例,展示如何在 XAML 中使用 Rectangle 控件:
XML<Window x:Class="AppRectangle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AppRectangle"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Rectangle Width="200" Height="100"
Fill="LightBlue"
Stroke="DarkBlue"
StrokeThickness="2"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</Window>
在此示例中:
Rectangle,无需像 WinForm 那样在 Paint 方法中手动绘制。Fill 用于控制矩形内部的填充颜色,Stroke 和 StrokeThickness 用于控制边框颜色及粗细。HorizontalAlignment、VerticalAlignment 等属性可控制矩形在父容器 Grid 中的对齐方式。MinWidth, MaxWidth 等布局特性配合使用。Brush,也可以是线性渐变(LinearGradientBrush)或图像填充(ImageBrush)。XML<Rectangle Width="150" Height="80"
RadiusX="10" RadiusY="10"
Fill="LightGreen"
Stroke="Green"
StrokeThickness="2" />

如果要在多个地方使用相同的矩形样式或配置,可以将相关信息放入资源中。同样,如果矩形的某些属性需要跟随数据变化,可以使用 WPF 的数据绑定机制。
XML<Window x:Class="AppRectangle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AppRectangle"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<!-- 定义一个Brush资源 -->
<SolidColorBrush x:Key="RectFillBrush" Color="LightPink" />
</Window.Resources>
<Grid>
<Rectangle Width="150" Height="80" RadiusX="10" RadiusY="10"
Fill="{StaticResource RectFillBrush}" Stroke="Green" StrokeThickness="2"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Window>
如果你需要在应用运行时动态切换某个颜色或对象,可以使用 DynamicResource。它会在资源状态变更后自动更新到界面:
XML<Rectangle Width="200" Height="100"
Fill="{DynamicResource RectFillBrush}" />
可以将 Rectangle 的属性绑定到后端数据源中,让矩形动态随业务逻辑而改变,比如根据数值调节矩形宽度。
XML<Rectangle Fill="LightCoral"
Width="{Binding RectangleWidth}"
Height="50"
HorizontalAlignment="Center" />
C#public partial class MainWindow : Window
{
public int RectangleWidth { get; set; } = 200;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
}
绑定对应的 RectangleWidth 属性,在后台 ViewModel 内更新该属性就可自动调整 UI。
在 WPF 中,Style 可以让我们轻松定制批量的控件外观。下面的示例通过 Style 统一配置 Rectangle 的填充、边框等属性。
XML<Window x:Class="AppRectangle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AppRectangle"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<!-- 统一样式 -->
<Style x:Key="RectangleStyle" TargetType="Rectangle">
<Setter Property="Fill" Value="LightGray"/>
<Setter Property="Stroke" Value="DimGray"/>
<Setter Property="StrokeThickness" Value="2"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="60"/>
<Setter Property="Margin" Value="5"/>
</Style>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Rectangle Style="{StaticResource RectangleStyle}" />
<Rectangle Style="{StaticResource RectangleStyle}" Fill="LightBlue" />
<Rectangle Style="{StaticResource RectangleStyle}" Stroke="Red" />
</StackPanel>
</Grid>
</Window>

通过该样式,多个 Rectangle 可以共用一组默认配置,而也可以通过在局部覆盖某些属性来实现个性化差异。
WPF 通过 Triggers 再结合 Storyboard 可以轻松给矩形加上动态效果,比如鼠标悬停时颜色渐变,或点击时尺寸变化等。
下面的示例使用 Style.Triggers 让 Rectangle 在鼠标悬浮时填充色发生淡入淡出动画。
XML<Window x:Class="AppRectangle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AppRectangle"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="HoverRectangleStyle" TargetType="Rectangle">
<Setter Property="Fill" Value="LightGray" />
<Setter Property="Stroke" Value="Black" />
<Setter Property="StrokeThickness" Value="2" />
<Setter Property="Margin" Value="5" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="100" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<!-- 颜色渐变动画 -->
<ColorAnimation
Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)"
To="Orange"
Duration="0:0:0.3"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<!-- 还原回原色 -->
<ColorAnimation
Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)"
To="LightGray"
Duration="0:0:0.3"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Rectangle Style="{StaticResource HoverRectangleStyle}"/>
<Rectangle Style="{StaticResource HoverRectangleStyle}" Fill="LightBlue" />
<Rectangle Style="{StaticResource HoverRectangleStyle}" Stroke="Red"/>
</StackPanel>
</Grid>
</Window>

通过这种方式,你可以很容易地将简单的矩形变成一个带有鼠标交互效果的视觉元素。
Paint 事件来绘制矩形,而在 WPF 中直接使用 Rectangle 控件就能轻松实现绘图功能,并且能够通过依赖属性、样式和触发器让效果更加灵活。Rectangle 作为 Shape 的派生类,会自动参与到 WPF 的布局系统,加之数据绑定、资源管理和动画系统,使得开发者更容易实现动态化、可交互的 UI。本文作者:技术老小子
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!