wpf 控件开发基础(6) -单一容器(Decorator)

前端之家收集整理的这篇文章主要介绍了wpf 控件开发基础(6) -单一容器(Decorator)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

其实这部分的文章已经很多了,写下来方便自己查询.

wpf内置提供了很多容器(Panel),容器分为多容器和单容器.下面介绍单容器.内置的单容器,大家最熟悉的如Border,其作用用于装饰容器内的元素,单一容器继承自Decorator,下面来看一个未使用装饰器的例子.

<@H_404_6@Window @H_404_6@@H_301_9@x@H_404_6@:@H_404_6@@H_301_9@Class@H_404_6@="WPFControlTutorialPart6_WPFApp.Window1"
    @H_404_6@@H_301_9@xmlns@H_404_6@="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    @H_404_6@@H_301_9@xmlns@H_404_6@:@H_404_6@@H_301_9@x@H_404_6@="http://schemas.microsoft.com/winfx/2006/xaml"
    @H_404_6@@H_301_9@xmlns@H_404_6@:@H_404_6@@H_301_9@control@H_404_6@="clr-namespace:WPF.Controls;assembly=WPF.Controls"
    @H_404_6@@H_301_9@Title@H_404_6@="Window1" @H_404_6@@H_301_9@Height@H_404_6@="300" @H_404_6@@H_301_9@Width@H_404_6@="300">
    <@H_404_6@Grid@H_404_6@>
            <@H_404_6@Button@H_404_6@>@H_404_6@Hello World@H_404_6@</@H_404_6@Button@H_404_6@>
    </@H_404_6@Grid@H_404_6@>
</@H_404_6@Window@H_404_6@>@H_404_6@

可以看到一个Button控件填充了整个窗体。一般我们只需要显示正常大小的Button即可.

容器计算规则

计算容器的大小是一个递归的过程,永远是先测量(MeasureOverride)子元素,然后通知父元素分配空间.计算好空间后然后排列(ArrangeOverride),如果测量大小过度ArrangeOverride自动进行修正的.自定义一个单容器来修正上面的布局

public class @H_404_6@PixelSnapper @H_404_6@: Decorator
@H_404_6@{

    protected override @H_404_6@Size @H_404_6@ArrangeOverride(Size @H_404_6@finalSize)
    {
        Point @H_404_6@location = this@H_404_6@.CalculateOffset(finalSize,this@H_404_6@.Child);
        location.X = Math@H_404_6@.Round(location.X);
        location.Y = Math@H_404_6@.Round(location.Y);
        this@H_404_6@.Child.Arrange(new @H_404_6@Rect@H_404_6@(location,this@H_404_6@.Child.DesiredSize));
        return @H_404_6@finalSize;
    }

    private @H_404_6@Point @H_404_6@CalculateOffset(Size @H_404_6@finalSize,UIElement @H_404_6@ui)
    {
        Point @H_404_6@point = new @H_404_6@Point@H_404_6@(0.0,0.0);
        if @H_404_6@(ui is @H_404_6@FrameworkElement@H_404_6@)
        {
            FrameworkElement @H_404_6@element = ui as @H_404_6@FrameworkElement@H_404_6@;
            Size @H_404_6@desiredSize = element.DesiredSize;
            switch @H_404_6@(element.HorizontalAlignment)
            {
                case @H_404_6@HorizontalAlignment@H_404_6@.Left:
                    point.X = element.Margin.Left;
                    break@H_404_6@;

                case @H_404_6@HorizontalAlignment@H_404_6@.Center:
                    point.X = (((finalSize.Width - desiredSize.Width) + base@H_404_6@.Margin.Left) - base@H_404_6@.Margin.Right) / 2.0;
                    break@H_404_6@;

                case @H_404_6@HorizontalAlignment@H_404_6@.Right:
                    point.X = (finalSize.Width - element.Margin.Right) - desiredSize.Width;
                    break@H_404_6@;
            }
            switch @H_404_6@(element.VerticalAlignment)
            {
                case @H_404_6@VerticalAlignment@H_404_6@.Top:
                    point.Y = element.Margin.Top;
                    return @H_404_6@point;

                case @H_404_6@VerticalAlignment@H_404_6@.Center:
                    point.Y = (((finalSize.Height - desiredSize.Height) + base@H_404_6@.Margin.Top) - base@H_404_6@.Margin.Bottom) / 2.0;
                    return @H_404_6@point;

                case @H_404_6@VerticalAlignment@H_404_6@.Bottom:
                    point.Y = (finalSize.Height - element.Margin.Bottom) - desiredSize.Height;
                    return @H_404_6@point;
            }
        }
        return @H_404_6@point;
    }
}

现在布局

wpf内置的BulletDecorator.如内置的CheckBox和RadioButton都是BulletDecorator,左边是符号,右侧是Content

示例

猜你在找的设计模式相关文章