博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF自定义控件(五)の用户控件(完结)
阅读量:5139 次
发布时间:2019-06-13

本文共 11286 字,大约阅读时间需要 37 分钟。

原文:

用户控件,WPF中是继承自UserControl的控件,我们可以在里面融合我们的业务逻辑。

示例:(一个厌恶选择的用户控件)

后端:

using iMicClassBase;using iMicClassBase.BaseControl;using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Forms;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace iMicClassUI.HomeToolControls{    /**     * @控件名:HomeToolBackgroundPanel     * @功能  :切换黑板界面背景     * @作者  :tjer     * @时间  :2017.05.19     * **/    ///     /// HomeToolBackgroundPanel.xaml 的交互逻辑    ///     public partial class HomeToolBackgroundPanel : System.Windows.Controls.UserControl    {        #region 委托及事件        ///         /// 背景改变事件委托        ///         /// 源        /// 类型        /// 改变对象        public delegate void BackGroundSelectedChangeDelegate(object sender, BBBackGroundType type,object ChangeObj);        ///         /// 背景改变事件        ///         public BackGroundSelectedChangeDelegate BackGroundSelectedChange = null;        ///         /// 选项卡改变事件        ///         public static readonly RoutedEvent TabItemChangeEvent = EventManager.RegisterRoutedEvent        ("TabItemChange", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(HomeToolBackgroundPanel));        public event RoutedEventHandler TabItemChange        {            add { AddHandler(TabItemChangeEvent, value); }            remove { RemoveHandler(TabItemChangeEvent, value); }        }        #endregion        #region 变量        ///         /// 调用系统颜色框        ///         private ColorDialog SysColorDia = new ColorDialog();        #endregion        #region 属性及依赖属性        ///         /// 当前选项卡背景类型        ///         public BBBackGroundType SelectedOption        {            get;            set;        } = BBBackGroundType.ColorType;        ///         /// 当前颜色        ///         public static readonly DependencyProperty CurrentColorProperty =            DependencyProperty.Register("CurrentColor", typeof(System.Windows.Media.Color), typeof(HomeToolBackgroundPanel), new PropertyMetadata((Color)ColorConverter.ConvertFromString("#FF18311B"), OnCurrentColorChanged));        [Description("当前颜色"), Browsable(false)]                public System.Windows.Media.Color CurrentColor        {            get { return (System.Windows.Media.Color)GetValue (CurrentColorProperty); }            set { SetValue(CurrentColorProperty, value); }        }        ///         /// 当前本地图片背景,从1开始,默认是无效0        ///         public static readonly DependencyProperty CurrentSysBgIndexProperty =            DependencyProperty.Register("CurrentSysBgIndex", typeof(int), typeof(HomeToolBackgroundPanel), new PropertyMetadata(0, OnCurrentSysBgIndexChanged));        [Description("当前系统图片背景索引"), Browsable(false)]        public int CurrentSysBgIndex        {            get { return (int)GetValue(CurrentSysBgIndexProperty); }            set { SetValue(CurrentSysBgIndexProperty ,value); }        }        ///         /// 当前本地图片路径        ///         public static readonly DependencyProperty CurrentLocalImageBgURIProperty =            DependencyProperty.Register("CurrentLocalImageBgURI", typeof(string), typeof(HomeToolBackgroundPanel), new PropertyMetadata(string.Empty, OnCurrentLocalImageBgURIChanged));        [Description("当前本地图片路径"), Browsable(false)]        public string CurrentLocalImageBgURI        {            get { return (string)GetValue(CurrentLocalImageBgURIProperty); }            set { SetValue(CurrentLocalImageBgURIProperty, value); }        }        #endregion        #region 窗体事件        ///         /// 构造        ///         public HomeToolBackgroundPanel()        {            InitializeComponent();        }        ///         /// 初始化,每次显示的时候调用,可以展示当前黑板背景状态        ///         /// 当前黑板背景类型        /// 背景内容对象        public void Init(BBBackGroundType type,object obj)        {            switch (type)            {                case BBBackGroundType.ColorType:                    tabPanel.SelectedIndex = 0;                    CurrentColor = (System.Windows.Media.Color)obj ;                    break;                case BBBackGroundType.ImageType:                    tabPanel.SelectedIndex = 1;                    CurrentSysBgIndex = (int)obj;                    break;                case BBBackGroundType.LocalType:                    tabPanel.SelectedIndex = 2;                    CurrentLocalImageBgURI = (string)obj;                    break;            }        }        #endregion        #region 依赖事件        ///         /// 颜色改变事件        ///         /// 依赖对象        /// 事件        private static void OnCurrentColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            if (e.OldValue == e.NewValue)            {                return;            }            if ((Color)e.NewValue== (Color)ColorConverter.ConvertFromString("#FF18311B"))            {                return;            }            HomeToolBackgroundPanel control = (HomeToolBackgroundPanel)d;            control.CurrentSysBgIndex = 0;            control.CurrentLocalImageBgURI = string.Empty;            control.BackGroundSelectedChange?.Invoke(control, BBBackGroundType.ColorType,control.CurrentColor);        }         ///          /// 背景索引改变事件         ///          /// 依赖对象         /// 事件        private static void OnCurrentSysBgIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            if (e.OldValue == e.NewValue)            {                return;            }            if ((int)e.NewValue==0)            {                return;            }                               HomeToolBackgroundPanel control = (HomeToolBackgroundPanel)d;            control.CurrentColor = (Color)ColorConverter.ConvertFromString("#FF18311B");             control.CurrentLocalImageBgURI = string.Empty;            control.BackGroundSelectedChange?.Invoke(control, BBBackGroundType.ImageType, control.CurrentSysBgIndex);        }        ///         /// 本地背景路径改变        ///         /// 依赖对象        /// 事件        private static void OnCurrentLocalImageBgURIChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            if (e.OldValue == e.NewValue)            {                return;            }            if ((string)e.NewValue == string.Empty)            {                return;            }            HomeToolBackgroundPanel control = (HomeToolBackgroundPanel)d;            control.CurrentColor = (Color)ColorConverter.ConvertFromString("#FF18311B");            control.CurrentSysBgIndex = 0;            control.BackGroundSelectedChange?.Invoke(control, BBBackGroundType.LocalType, control.CurrentLocalImageBgURI);        }        #endregion        #region 内部操作事件        ///         /// 选项卡改变事件        ///         /// 事件源        /// 事件        private void TabItem_Select(object sender, RoutedEventArgs e)        {            //选项卡改变,背景类型改变            switch (tabPanel.SelectedIndex)            {                case 0:                    SelectedOption = BBBackGroundType.ColorType;                    break;                case 1:                    SelectedOption = BBBackGroundType.ImageType;                    break;                case 2:                    SelectedOption = BBBackGroundType.LocalType;                    break;            }            RoutedEventArgs routedEventArgs = new RoutedEventArgs(TabItemChangeEvent, e.OriginalSource);            RaiseEvent(routedEventArgs);        }        ///         /// 面板颜色值选择        ///         /// 事件源        /// 事件        private void ColorPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)        {            colorPanelCurrent.Color = (sender as ColorPanel).Color;            if ((sender as ColorPanel).Color == (Color)(ColorConverter.ConvertFromString("#FF18311B")))     //保证颜色值是设定色的时候也会触发            {                btnBg1.IsSelected = false;                btnBg2.IsSelected = false;                btnBg3.IsSelected = false;                btnBg4.IsSelected = false;                CurrentSysBgIndex = 0;                CurrentLocalImageBgURI = string.Empty;                BackGroundSelectedChange?.Invoke(this, BBBackGroundType.ColorType, CurrentColor);            }        }        ///         /// 点击更多颜色        ///         /// 事件源        /// 事件        private void btnMoreColor_Click(object sender, RoutedEventArgs e)        {            DialogResult result = SysColorDia.ShowDialog();            if (result == System.Windows.Forms.DialogResult.OK)            {                colorPanelCurrent.Color = ColorConverterHelp.ConvertToMediaColor(SysColorDia.Color);            }        }        ///         /// 本地背景选择        ///         /// 事件源        /// 事件        private void SelectButton_ButtonSelected(object sender, EventArgs e)        {            int index = Convert.ToInt32((sender as SelectButton).Tag);            switch (index)            {                case 1:                    btnBg2.IsSelected = false;                    btnBg3.IsSelected = false;                    btnBg4.IsSelected = false;                    CurrentSysBgIndex = 1;                    break;                case 2:                    btnBg1.IsSelected = false;                    btnBg3.IsSelected = false;                    btnBg4.IsSelected = false;                    CurrentSysBgIndex = 2;                    break;                case 3:                    btnBg1.IsSelected = false;                    btnBg2.IsSelected = false;                    btnBg4.IsSelected = false;                    CurrentSysBgIndex = 3;                    break;                case 4:                    btnBg1.IsSelected = false;                    btnBg2.IsSelected = false;                    btnBg3.IsSelected = false;                    CurrentSysBgIndex = 4;                    break;            }        }        ///         /// 点击选择本地图片        ///         ///         ///         private void btnAddLoaclImage_Click(object sender, RoutedEventArgs e)        {            OpenFileDialog localBg = new OpenFileDialog();            localBg.DefaultExt = ".png";            localBg.Filter = "(背景)|*.png";            if (localBg.ShowDialog() == DialogResult.OK)            {                btnBg1.IsSelected = false;                btnBg2.IsSelected = false;                btnBg3.IsSelected = false;                btnBg4.IsSelected = false;                CurrentSysBgIndex = 0;                CurrentLocalImageBgURI = System.IO.Path.GetFullPath(localBg.FileName);            }        }        #endregion    }}

  前端:

  

其实用户控件是我们常用的,没什么可说的,在此做个说明,只想保持博文队形整齐。

自定义控件系列博文链接:

 

posted on
2018-11-11 13:12 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lonelyxmas/p/9941831.html

你可能感兴趣的文章
SQL更新某列包含XX的所有值
查看>>
网易味央第二座猪场落户江西 面积超过3300亩
查看>>
面试时被问到的问题
查看>>
spring 事务管理
查看>>
VS2008 去掉msvcr90的依赖
查看>>
当前记录已被另一个用户锁定
查看>>
Node.js 连接 MySQL
查看>>
那些年,那些书
查看>>
注解小结
查看>>
java代码编译与C/C++代码编译的区别
查看>>
Bitmap 算法
查看>>
转载 C#文件中GetCommandLineArgs()
查看>>
list control控件的一些操作
查看>>
LVM快照(snapshot)备份
查看>>
绝望的第四周作业
查看>>
一月流水账
查看>>
npm 常用指令
查看>>
非常棒的Visual Studo调试插件:OzCode 2.0 下载地址
查看>>
判断字符串在字符串中
查看>>
Linux环境下Redis安装和常见问题的解决
查看>>