当前位置: 首页 > news >正文

[C#] WPF - 自定义控件(行列间距UniformGrid)

一、创建

1、创建“自定义控件” SpaceUniformGrid,继承UniformGrid。

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;namespace WpfApp1
{internal class SpaceUniformGrid : UniformGrid{#region RowSpacepublic double RowSpace{get { return (double)GetValue(RowSpaceProperty); }set { SetValue(RowSpaceProperty, value); }}public static readonly DependencyProperty RowSpaceProperty =DependencyProperty.Register("RowSpace", typeof(double), typeof(SpaceUniformGrid), new PropertyMetadata((double)0, OnRowSpacePropertyChanged));private static void OnRowSpacePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){if (d is SpaceUniformGrid sug){sug.InvalidateArrange();}}#endregion#region ColumnSpacepublic double ColumnSpace{get { return (double)GetValue(ColumnSpaceProperty); }set { SetValue(ColumnSpaceProperty, value); }}public static readonly DependencyProperty ColumnSpaceProperty =DependencyProperty.Register("ColumnSpace", typeof(double), typeof(SpaceUniformGrid), new PropertyMetadata((double)0, OnColumnSpacePropertyChanged));private static void OnColumnSpacePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){if (d is SpaceUniformGrid sug){sug.InvalidateArrange();}}#endregionprotected override Size ArrangeOverride(Size arrangeSize){int columnCount = 1;if (Rows > 0){columnCount = (int)Math.Ceiling((double)Children.Count / Rows);}if (Columns > 0){columnCount = Columns;}double childWidth = (arrangeSize.Width - ColumnSpace * (columnCount - 1)) / columnCount;double childHeight = Children.Cast<UIElement>().Select(p => p.DesiredSize.Height).Max();for (int i = 0; i < Children.Count; i++){double childX = (i % columnCount) * (childWidth + ColumnSpace);double childY = (i / columnCount) * (childHeight + RowSpace);Children[i].Arrange(new Rect(childX, childY, childWidth, Children[i].DesiredSize.Height));}return arrangeSize;}}
}

二、使用

1、MainWindow.xaml

<Windowx:Class="WpfApp1.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:local="clr-namespace:WpfApp1"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"Title="Study SpaceUniformGrid"Width="600"Height="400"mc:Ignorable="d"><Grid Margin="20"><Grid.RowDefinitions><RowDefinition Height="*" /><RowDefinition Height="*" /><RowDefinition Height="*" /></Grid.RowDefinitions><GroupBox Grid.Row="0" Header="行间距"><local:SpaceUniformGrid Columns="2" RowSpace="20"><Button Height="20" /><Button Height="20" /><Button Height="20" /><Button Height="20" /></local:SpaceUniformGrid></GroupBox><GroupBox Grid.Row="1" Header="列间距"><local:SpaceUniformGrid ColumnSpace="20" Rows="2"><Button Height="20" /><Button Height="20" /><Button Height="20" /><Button Height="20" /></local:SpaceUniformGrid></GroupBox><GroupBox Grid.Row="2" Header="行间距和列间距"><local:SpaceUniformGridColumnSpace="20"Columns="2"RowSpace="20"><Button Height="20" /><Button Height="20" /><Button Height="20" /><Button Height="20" /></local:SpaceUniformGrid></GroupBox></Grid>
</Window>

三、运行

http://www.dtcms.com/a/337014.html

相关文章:

  • docker compose再阿里云上无法使用的问题
  • 矿物分类系统开发笔记(一):数据预处理
  • 楼宇自控系统深化设计需关注哪些核心要点?技术与应用解析
  • Casadi库C++运行速度比python版本慢解决方法
  • 从第一性原理理解Embedding:独立模型vs大模型内嵌层的本质区别
  • linux应用软件编程:线程
  • 使用Idea安装JDK
  • 04.IO 重定向和管道
  • 【深度学习】pytorch深度学习框架的环境配置
  • 如何在服务器 clone github 项目
  • axure chrome 浏览器插件的使用
  • goland怎么取消自动删除未使用的包
  • 学习链接。
  • 【秋招笔试】2025.08.17字节跳动秋招机考真题
  • 论文阅读 2025-8-9 [DiC, DropKey]
  • C++中内存池(Memory Pool)详解和完整示例
  • python实现pdfs合并
  • Leetcode 深度优先搜索 (3)
  • WPF中BindingList<T>和List<T>
  • vue3 + antd实现简单的图片点开可以缩小放大查看
  • 浅谈 Python 正则表达式中的 groups()
  • Linux文件相关命令
  • Disbursement on Quarantine Policy(概率、逆元计算期望)
  • 寻北仪如何靠“小”征服地下世界?
  • AI需要防火墙,云计算需要重新构想
  • 20. 云计算-多租户
  • 猫头虎AI分享|一款智能量化交易系统:QuantCell,从数据收集到策略执行全流程自动化
  • C++ 多进程编程深度解析【C++进阶每日一学】
  • 人工智能技术爆发的时代,AI工具和大模型对行业的深刻变革及实际应用!
  • 面向R语言用户的Highcharts