wpf实现自定义布局

来源:互联网 发布:黄金时代知乎 编辑:程序博客网 时间:2024/06/05 23:04
<Window x:Class="CustomPanel.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:c="clr-namespace:CustomPanel"        mc:Ignorable="d"        Title="MainWindow" >    <DockPanel>        <TabControl DockPanel.Dock="Top" Height="130">            <TabItem>                <StackPanel Orientation="Horizontal">                    <GroupBox Header="Clipboard" Padding="2">                        <c:RibbonPanel>                            <Grid>                                <Grid.RowDefinitions>                                    <RowDefinition/>                                    <RowDefinition Height="auto"/>                                </Grid.RowDefinitions>                                <Rectangle Grid.Row="0"  Fill="Black" Width="20" Height="20"></Rectangle>                                <Label Margin="10,0" Grid.Row="1">Paste</Label>                            </Grid>                            <Rectangle Fill="Black" Width="10" Height="10" Margin="5"></Rectangle>                            <Rectangle Fill="Black" Width="10" Height="10"></Rectangle>                            <Rectangle Fill="Black" Width="10" Height="10"></Rectangle>                            <Rectangle Fill="Black" Width="10" Height="10"></Rectangle>                            <Rectangle Fill="Black" Width="10" Height="10"></Rectangle>                            <Rectangle Fill="Black" Width="10" Height="10"></Rectangle>                        </c:RibbonPanel>                    </GroupBox>                </StackPanel>            </TabItem>        </TabControl>    </DockPanel></Window>
using System;using System.Windows;using System.Windows.Controls;namespace CustomPanel{    public class RibbonPanel : Panel    {        protected override Size MeasureOverride(Size availableSize)        {            if (Children.Count < 1)                return new Size(0, 0);            UIElement firstChild = Children[0];            firstChild.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));            if (Children.Count < 2)                return firstChild.DesiredSize;            double numRows = Math.Ceiling((Children.Count - 1) / 3d);            double maxWidthForEachRemainingChild = 0;            for (int i = 1; i < Children.Count; ++i)            {                UIElement child = Children[i];                child.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));                maxWidthForEachRemainingChild = Math.Max(child.DesiredSize.Width, maxWidthForEachRemainingChild);            }            return new Size(firstChild.DesiredSize.Width + maxWidthForEachRemainingChild * numRows, firstChild.DesiredSize.Height);        }        protected override Size ArrangeOverride(Size finalSize)        {            if (Children.Count < 1)                return finalSize;            UIElement firstChild = Children[0];            Point childOrigin = new Point(0, 0);            Size firstChildSize = new Size(firstChild.DesiredSize.Width, finalSize.Height);            firstChild.Arrange(new Rect(childOrigin, firstChildSize));            if (Children.Count < 2)                return finalSize;            double numRows = Math.Ceiling((Children.Count - 1) / 3d);            Size childSize = new Size((finalSize.Width - firstChildSize.Width) / numRows, finalSize.Height / 3);            childOrigin.X += firstChildSize.Width;            for (int i = 1; i < Children.Count; ++i)            {                UIElement child = Children[i];                child.Arrange(new Rect(childOrigin, childSize));                if (i % 3 == 0)                {                    childOrigin.X += childSize.Width;                    childOrigin.Y = 0;                }                else                    childOrigin.Y += childSize.Height;            }            return finalSize;        }    }}
原创粉丝点击