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

发布于:2025-08-19 ⋅ 阅读:(15) ⋅ 点赞:(0)

一、创建

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

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace WpfApp1
{
	internal class SpaceUniformGrid : UniformGrid
	{
		#region RowSpace
		public 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 ColumnSpace
		public 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();
			}
		}
		#endregion

		protected 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

<Window
    x: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:SpaceUniformGrid
                ColumnSpace="20"
                Columns="2"
                RowSpace="20">
                <Button Height="20" />
                <Button Height="20" />
                <Button Height="20" />
                <Button Height="20" />
            </local:SpaceUniformGrid>
        </GroupBox>
    </Grid>
</Window>

三、运行


网站公告

今日签到

点亮在社区的每一天
去签到