WPF自定义组件及其依赖属性+刷新组件

WPF自定义组件及其依赖属性+刷新组件

因项目需求,需要做一些自定义组件,纯小白操作,希望能够帮助到大家,注释比较清晰,直接上代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Railway.Custom.UI.Controls
{
    /// <summary>
    ///自定义箭头控件,
    /// </summary>
    public class Arrow : Control
    {
        static Arrow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Arrow), new FrameworkPropertyMetadata(typeof(Arrow)));
        }

        #region 声明自定义属性

        #endregion

        #region 自定义属性get/set
        /// <summary>
        /// 箭头颜色
        /// </summary>
        public SolidColorBrush ArrowColor
        {
            get { return (SolidColorBrush)GetValue(ArrowColorProperty); }
            set { SetValue(ArrowColorProperty, value); }
        }
        /// <summary>
        /// 箭头边框颜色
        /// </summary>
        public SolidColorBrush ArrowBorderColor
        {
            get { return (SolidColorBrush)GetValue(ArrowBorderColorProperty); }
            set { SetValue(ArrowBorderColorProperty, value); }           
        }
        /// <summary>
        /// 箭头边框宽度
        /// </summary>
        public double ArrowBorder
        {
            get { return (double)GetValue(ArrowBorderProperty); }
            set { SetValue(ArrowBorderProperty, value); }
        }
        /// <summary>
        /// 箭头方向
        /// </summary>
        public string Direction
        {
            get { return (string)GetValue(DirectionProperty); }
            set { SetValue(DirectionProperty, value); }
        }
        #endregion

        #region 注册事件及依赖属性
        public static readonly DependencyProperty ArrowColorProperty =
            DependencyProperty.Register("ArrowColor", typeof(SolidColorBrush), typeof(Arrow), new PropertyMetadata(new SolidColorBrush(System.Windows.Media.Color.FromRgb(145, 150, 150)), new PropertyChangedCallback(ChangeArrowColor)));

        public static readonly DependencyProperty ArrowBorderColorProperty =
            DependencyProperty.Register("ArrowBorderColor", typeof(SolidColorBrush), typeof(Arrow), new PropertyMetadata(new SolidColorBrush(System.Windows.Media.Color.FromRgb(211,211,211)), new PropertyChangedCallback(ChangeArrowBorderColor)));

        public static readonly DependencyProperty ArrowBorderProperty =
            DependencyProperty.Register("ArrowBorder", typeof(double), typeof(Arrow), new PropertyMetadata((double)2, new PropertyChangedCallback(ChangeArrowBorder)));

        public static readonly DependencyProperty DirectionProperty =
            DependencyProperty.Register("Direction", typeof(string), typeof(Arrow), new PropertyMetadata("Left", new PropertyChangedCallback(ChangeDirection)));
        #endregion


        #region 注册事件及依赖属性回调方法

        /// <summary>
        /// 重写 OnRender 方法
        /// </summary>
        /// <param name="drawingContext"></param>
        protected override void OnRender(DrawingContext drawingContext)
        {
            drawingContext.DrawGeometry(ArrowColor, new Pen(ArrowBorderColor, ArrowBorder), RenderDraw());
        }
        /// <summary>
        /// 改箭头颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void ChangeArrowColor(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            //新数据
            SolidColorBrush newValue = (SolidColorBrush)e.NewValue;
            //旧数据
            SolidColorBrush oldValue = (SolidColorBrush)e.OldValue;

            if (newValue != oldValue)
            {
                var obj = (Arrow)sender;
                obj.ArrowColor = newValue;
                //刷新组件
                obj.InvalidateVisual();
            }
        }
        /// <summary>
        /// 改边框颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void ChangeArrowBorderColor(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            //新数据
            SolidColorBrush newValue = (SolidColorBrush)e.NewValue;
            //旧数据
            SolidColorBrush oldValue = (SolidColorBrush)e.OldValue;

            if (newValue != oldValue)
            {
                var obj = (Arrow)sender;
                obj.ArrowBorderColor = newValue;
                obj.InvalidateVisual();
            }
        }
        /// <summary>
        /// 改边框粗细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void ChangeArrowBorder(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            //新数据
            double newValue = (double)e.NewValue;
            //旧数据
            double oldValue = (double)e.OldValue;

            if (newValue != oldValue)
            {
                var obj = (Arrow)sender;
                obj.ArrowBorder = newValue;
                obj.InvalidateVisual();
            }
        }
        /// <summary>
        /// 改方向
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void ChangeDirection(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            //新数据
            string newValue = (string)e.NewValue;
            //旧数据
            string oldValue = (string)e.OldValue;

            if (newValue != oldValue)
            {
                var obj = (Arrow)sender;
                obj.Direction = newValue;
                obj.InvalidateVisual();
            }
        }
        

        /// <summary>
        /// 画箭头
        /// </summary>
        /// <returns></returns>
        private StreamGeometry RenderDraw()
        {
            StreamGeometry streamGeometry = new StreamGeometry();
            Point point0;
            Point point1;
            Point point2;
            Point point3;
            Point point4;
            Point point5;
            Point point6;
            switch (Direction.ToLower())
            {
                case "top":
                    point0 = new Point(ActualWidth / 3, ActualHeight);
                    point1 = new Point(ActualWidth / 3 * 2, ActualHeight);
                    point2 = new Point(ActualWidth / 3 * 2, ActualHeight / 3);
                    point3 = new Point(ActualWidth, ActualHeight / 3);
                    point4 = new Point(ActualWidth/2, 0);
                    point5 = new Point(0, ActualHeight / 3);
                    point6 = new Point(ActualWidth / 3, ActualHeight / 3);
                    break;
                case "down":
                    point0 = new Point(ActualWidth / 3, 0);
                    point1 = new Point(ActualWidth / 3 * 2, 0);
                    point2 = new Point(ActualWidth / 3 * 2, ActualHeight / 3 * 2);
                    point3 = new Point(ActualWidth, ActualHeight / 3 * 2);
                    point4 = new Point(ActualWidth / 2, ActualHeight);
                    point5 = new Point(0, ActualHeight / 3 * 2);
                    point6 = new Point(ActualWidth / 3, ActualHeight / 3 * 2);
                    break;
                case "right":
                    point0 = new Point(0, ActualHeight / 3);
                    point1 = new Point(ActualWidth / 3 * 2, ActualHeight / 3);
                    point2 = new Point(ActualWidth / 3 * 2, 0);
                    point3 = new Point(ActualWidth, ActualHeight / 2);
                    point4 = new Point(ActualWidth / 3 * 2, ActualHeight);
                    point5 = new Point(ActualWidth / 3 * 2, ActualHeight / 3 * 2);
                    point6 = new Point(0, ActualHeight / 3 * 2);
                    break;
                case "left":
                default:
                    point0 = new Point(ActualWidth / 3, 0);
                    point1 = new Point(ActualWidth / 3, ActualHeight / 3);
                    point2 = new Point(ActualWidth, ActualHeight / 3);
                    point3 = new Point(ActualWidth, ActualHeight / 3 *2);
                    point4 = new Point(ActualWidth / 3, ActualHeight/3*2);
                    point5 = new Point(ActualWidth / 3 , ActualHeight);
                    point6 = new Point(0, ActualHeight / 2);
                    break;

            }
            using (StreamGeometryContext streamGeometryContext = streamGeometry.Open())
            {
                streamGeometryContext.BeginFigure(point0, true, false);
                streamGeometryContext.LineTo(point1, true, true);
                streamGeometryContext.LineTo(point2, true, true);
                streamGeometryContext.LineTo(point3, true, true);
                streamGeometryContext.LineTo(point4, true, true);
                streamGeometryContext.LineTo(point5, true, true);
                streamGeometryContext.LineTo(point6, true, true);
                streamGeometryContext.LineTo(point0, true, true);
            }
            return streamGeometry;
        }
        #endregion
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值