2025-10-06
C#
00

在从 WinForm 向 WPF 转型的过程中,很多开发者都会思考如何在 WPF 中实现各种自定义图形的绘制。WinForm 中通常通过 GDI+或者直接在控件的 Paint 事件中手动绘图。而在 WPF 中,使用 Shape 派生类(例如 Rectangle, Ellipse, Line 等)就可以更直观地在界面上绘制图形。本篇文章将介绍如何在 WPF 中使用 Rectangle,并说明与 WinForm 的主要差异,以及如何利用 WPF 的强大样式和动画特性让矩形图形更灵活多变。

Rectangle 在 WinForm 与 WPF 中的差异

WinForm:

  • 通常需要在 OnPaintPaint 事件内通过 Graphics 对象手动调用 DrawRectangle 等方法进行绘制。
  • 若想改变矩形的位置、大小或颜色,通常需要触发重绘(Invalidate / Refresh)或联动其它 UI 事件。

WPF:

  • 通过 System.Windows.Shapes.Rectangle 控件来绘制矩形。可以把它看作一个 UIElement,天然支持布局系统。
  • 可直接在 XAML 中声明,或在后台代码创建并添加到容器中。
  • 可以使用依赖属性(如 Width, Height, Fill, Stroke 等)进行绑定或使用样式,轻松调整或动画化。
  • 几乎无需手动处理重绘,WPF 会自动计算并渲染界面。

一个简单的 Rectangle 示例

下面是一个最基础的示例,展示如何在 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>

image.png 在此示例中:

2025-10-06
C#
00

从WinForm转型到WPF开发时,最显著的变化之一就是布局系统的差异。WPF提供了更加灵活和强大的响应式布局能力,本文将详细介绍WPF中实现响应式布局的多种方式。

Grid布局 - 使用星号和Auto实现响应式

Grid是WPF中最灵活的布局容器之一,通过设置列宽和行高的比例,可以轻松实现响应式布局。

XML
<Window x:Class="AppResponsive.Window1" 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:AppResponsive" mc:Ignorable="d" Title="Window1" Height="450" Width="800"> <DockPanel LastChildFill="True"> <!-- 顶部工具栏 --> <StackPanel DockPanel.Dock="Top" Background="LightBlue" Height="50"> <TextBlock Text="顶部工具栏" VerticalAlignment="Center" HorizontalAlignment="Center"/> </StackPanel> <!-- 左侧导航 --> <StackPanel DockPanel.Dock="Left" Background="LightGreen" Width="150"> <TextBlock Text="左侧导航" VerticalAlignment="Center" HorizontalAlignment="Center"/> </StackPanel> <!-- 右侧面板 --> <StackPanel DockPanel.Dock="Right" Background="LightPink" Width="200"> <TextBlock Text="右侧面板" VerticalAlignment="Center" HorizontalAlignment="Center"/> </StackPanel> <!-- 底部状态栏 --> <StackPanel DockPanel.Dock="Bottom" Background="LightGray" Height="30"> <TextBlock Text="底部状态栏" VerticalAlignment="Center" HorizontalAlignment="Center"/> </StackPanel> <!-- 主内容区域(自动填充剩余空间) --> <Grid Background="White"> <TextBlock Text="主内容区域" VerticalAlignment="Center" HorizontalAlignment="Center"/> </Grid> </DockPanel> </Window>

image.png

2025-10-06
C#
00

前言

从WinForm转向WPF开发时,最需要转变的思维就是布局方式。WPF提供了更加灵活和强大的布局系统,可以轻松实现自适应布局设计。本文将详细介绍WPF中常用的布局控件及其应用。

Grid布局 - 最常用的布局控件

Grid是WPF中最灵活的布局控件,类似于HTML中的表格布局。它允许你定义行和列,并可以设置比例或固定大小。

基础Grid示例

XML
<Grid> <Grid.RowDefinitions> <!-- 第一行高度为Auto,根据内容自动调整 --> <RowDefinition Height="Auto"/> <!-- 第二行高度为* ,占用剩余所有空间 --> <RowDefinition Height="*"/> <!-- 第三行固定高度50 --> <RowDefinition Height="50"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <!-- 第一列宽度占比1 --> <ColumnDefinition Width="*"/> <!-- 第二列宽度占比2 --> <ColumnDefinition Width="2*"/> </Grid.ColumnDefinitions> <!-- 放置在第0行第0列的按钮 --> <Button Grid.Row="0" Grid.Column="0" Content="按钮1"/> <!-- 放置在第0行,跨越2列的文本块 --> <TextBlock Grid.Row="0" Grid.Column="1" Text="标题"/> <!-- 放置在第1行,跨越2列的列表 --> <ListBox Grid.Row="1" Grid.ColumnSpan="2"> <ListBoxItem>项目1</ListBoxItem> <ListBoxItem>项目2</ListBoxItem> </ListBox> </Grid>

image.png

2025-10-05
C#
00

前言

从WinForm迁移到WPF是很多.NET开发者都会经历的过程。WPF提供了更强大和灵活的布局系统,但对于习惯了WinForm的开发者来说,可能需要一些时间来适应。本文将详细介绍WPF布局嵌套的核心技巧。

WPF布局容器概述

WPF主要的布局容器包括:

  • Grid(网格布局)
  • StackPanel(堆栈布局)
  • DockPanel(停靠布局)
  • WrapPanel(自动换行布局)
  • Canvas(绝对定位布局)

从WinForm思维转变到WPF思维

WinForm的局限性:

C#
// WinForm中的控件定位 button1.Location = new Point(100, 100); button1.Size = new Size(80, 25);

WPF的优势:

XML
<!-- WPF中的控件定位 --> <Button Margin="100,100,0,0" Width="80" Height="25"/>
2025-10-05
C#
00

在WinForms中使用OxyPlot创建热力图的详细指南。热力图是一种非常有效的可视化工具,用于表示二维数据的密度或强度。它通过颜色变化来展示数据的分布情况,非常适合用于展示温度分布、人口密度、金融数据等多种场景。

准备工作

首先,我们需要在项目中添加OxyPlot的引用。您可以通过NuGet包管理器来安装OxyPlot。在Visual Studio中,右击您的项目,选择"管理NuGet包",然后搜索并安装以下包:

  • OxyPlot.Core
  • OxyPlot.WindowsForms

安装完成后,在您的代码文件顶部添加以下using语句:

C#
using OxyPlot; using OxyPlot.Series; using OxyPlot.WindowsForms; using OxyPlot.Axes;

创建基本热力图

让我们从一个简单的热力图开始。假设我们要展示一个5x5的温度数据矩阵。

C#
public partial class Form1 : Form { public Form1() { InitializeComponent(); CreateBasicHeatMap(); } private void CreateBasicHeatMap() { var plotView = new PlotView(); plotView.Dock = DockStyle.Fill; var model = new PlotModel { Title = "基本热力图" }; // 创建热力图数据 double[,] data = new double[5, 5]; for (int x = 0; x < 5; x++) { for (int y = 0; y < 5; y++) { data[x, y] = Math.Sin(x * 0.5) * Math.Cos(y * 0.5); } } var heatMapSeries = new HeatMapSeries { X0 = 0, X1 = 4, Y0 = 0, Y1 = 4, Interpolate = true, RenderMethod = HeatMapRenderMethod.Bitmap, Data = data }; model.Series.Add(heatMapSeries); // 添加颜色轴 model.Axes.Add(new LinearColorAxis { Position = AxisPosition.Right, Palette = OxyPalettes.Rainbow(100) }); plotView.Model = model; this.Controls.Add(plotView); } }

image.png