WPF桌面端开发1-常用组件

2020-05-09 14:57:39 浏览数 (1)

基本组件

展示类

  • Border 边框 默认不支持裁剪内部
  • Button
  • RadioButton
  • Image
  • Label
  • TextBlock
  • ProgressBar

输入类

  • TextBox
  • RichTextBox
  • PasswordBox

默认的Border不能剪切内部元素,自定义border支持内部剪切

代码语言:javascript复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;

namespace ZJClassTool.Views
{
    public class ClippingBorder : Border
    {
        protected override void OnRender(DrawingContext dc)
        {
            OnApplyChildClip();
            base.OnRender(dc);
        }

        public override UIElement Child
        {
            get
            {
                return base.Child;
            }
            set
            {
                if (Child != value)
                {
                    if (Child != null)
                    {
                        // Restore original clipping
                        Child.SetValue(ClipProperty, _oldClip);
                    }

                    if (value != null)
                    {
                        _oldClip = value.ReadLocalValue(ClipProperty);
                    }
                    else
                    {
                        // If we dont set it to null we could leak a Geometry object
                        _oldClip = null;
                    }

                    base.Child = value;
                }
            }
        }

        protected virtual void OnApplyChildClip()
        {
            UIElement child = Child;
            if (child != null)
            {
                var top = Math.Max(CornerRadius.TopLeft, CornerRadius.TopRight);
                var bottom = Math.Max(CornerRadius.BottomLeft, CornerRadius.BottomRight);
                var max = Math.Max(top, bottom);
                var size = RenderSize;
                var width = size.Width - (BorderThickness.Left   BorderThickness.Right);
                var height = size.Height - (BorderThickness.Top   BorderThickness.Bottom);
                Geometry result = new RectangleGeometry
            (new Rect(0, 0, width, height), max, max);
                double halfWidth = width / 2;
                double halfHeight = height / 2;

                if (CornerRadius.TopLeft == 0)
                {
                    result = new CombinedGeometry(
                        GeometryCombineMode.Union,
                        result,
                        new RectangleGeometry(new Rect(0, 0, halfWidth, halfHeight))
                    );
                }

                if (CornerRadius.TopRight == 0)
                {
                    result = new CombinedGeometry(GeometryCombineMode.Union, result, new RectangleGeometry
                (new Rect(halfWidth, 0, halfWidth, halfHeight)));
                }

                if (CornerRadius.BottomLeft == 0)
                {
                    result = new CombinedGeometry
                  (GeometryCombineMode.Union, result, new RectangleGeometry
                  (new Rect(0, halfHeight, halfWidth, halfHeight)));
                }
                if (CornerRadius.BottomRight == 0)
                {
                    result = new CombinedGeometry
                  (GeometryCombineMode.Union, result, new RectangleGeometry
                  (new Rect(halfWidth, halfHeight, halfWidth, halfHeight)));
                }
                child.Clip = result;
            }
        }

        public void Update() { OnApplyChildClip(); }

        private RectangleGeometry _clipRect = new RectangleGeometry();
        private object _oldClip;
    }
}

静态容器

  • StackPanel 行列布局不能换行
  • WrapPanel 行列布局能换行
  • Grid 网格布局
  • DockPanel 东西南北中布局

动态容器

  • ItemsControl
  • ListBox
  • ListView
  • DataGrid

ItemsControl、ListBox和ListView

ListView继承于ListBox,ListBox继承于ItemsControl,所以后面的组件拥有前面组件的一切特性。

相同点:

  1. 这三个控件都是列表型控件,可以进行列表绑定(ItemsSource);
  2. 这三个控件均使用ItemsPresenter来展示列表信息;

不同点:

  1. ListBox 继承于ItemsControl,增加了一个Selector对象,ItemsControl中的Item是不支持选择的。而ListBox中Item是支持选择,并且可以单选,多选。
  2. ItemsControl是不包含水平和垂直方向的滚动条的。ListBox和ListView有水平和垂直方向滚动条。

ListView和DataGrid

  1. ListView 理解为只读控件,更像是一种View(视图),而DataGrid更倾向于编辑数据。
  2. DataGrid允许自动生成列,ListView不行。
  3. DataGrid有RowDetails元素, ListView没有。
  4. DataGrid支持所有ListView支持的东西。

0 人点赞