前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF桌面端开发1-常用组件

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

作者头像
码客说
发布2020-05-09 14:57:39
6940
发布2020-05-09 14:57:39
举报
文章被收录于专栏:码客

基本组件

展示类

  • 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支持的东西。
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基本组件
  • 静态容器
  • 动态容器
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档