吴佳鑫的个人专栏

当日事当日毕,没有任何借口

导航

silverlight 自定义布局容器

Silverlight中所谓的布局容器,其实都是继承自Panel,然后重写两个方法:

MeasureOverride:计算元素应该渲染的尺寸

ArrangeOverride:在具体的位置渲染元素

 

demo:

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace LayoutDemo
{
    public class TestPanel2 : Panel
    {
        public TestPanel2() { }

        protected override Size MeasureOverride(Size availableSize)
        {
            foreach (UIElement element in base.Children)
            {
                element.Measure(availableSize);
            }
            return base.MeasureOverride(availableSize);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            switch (this.ori)
            {
                case Orientation.Horizontal:
                    {
                        double num = finalSize.Height;
                        foreach (UIElement element in base.Children)
                        {
                            num -= element.DesiredSize.Height;
                            element.Arrange(new Rect(0.0, num, finalSize.Width, element.DesiredSize.Height));
                        }
                        break;
                    }
                case Orientation.Vertical:
                    {
                        double num2 = finalSize.Width;
                        foreach (UIElement element in base.Children)
                        {
                            num2 -= element.DesiredSize.Width;
                            element.Arrange(new Rect(num2, 0.0, element.DesiredSize.Width, finalSize.Height));
                        }
                        break;
                    }
            }
            return base.ArrangeOverride(finalSize);
        }


        protected static void OnLayoutChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            TestPanel2 base2 = sender as TestPanel2;
            if (base2 != null)
            {
                base2.RefleshUpdate();
            }
        }

        public void RefleshUpdate()
        {
            double num = base.ActualWidth.Equals(double.NaN) ? base.DesiredSize.Width : base.ActualWidth;
            double num2 = base.ActualHeight.Equals(double.NaN) ? base.DesiredSize.Height : base.ActualHeight;
            this.ArrangeOverride(new Size(num, num2));
        }

        public Orientation ori
        {
            get { return (Orientation)GetValue(oriProperty); }
            set { SetValue(oriProperty, value); }
        }

        public static readonly DependencyProperty oriProperty =
            DependencyProperty.Register("ori", typeof(Orientation), typeof(TestPanel), new PropertyMetadata(Orientation.Horizontal, OnValueChaged));


        private static void OnValueChaged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            TestPanel2 panel = sender as TestPanel2;
            if (panel != null)
            {
                panel.RefleshUpdate();
            }
        }

    }
}

 

posted on 2011-11-01 15:03  _eagle  阅读(736)  评论(0编辑  收藏  举报