系列文章目录
WPF自定义控件与样式(1)-矢量字体图标(iconfont)
WPF自定义控件与样式(2)-自定义按钮FButton
WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展
WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式
WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展
WPF自定义控件与样式(6)-ScrollViewer与ListBox自定义样式
WPF自定义控件与样式(7)-列表控件DataGrid与ListView自定义样式
WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu
WPF自定义控件与样式(10)-进度控件ProcessBar自定义样
WPF自定义控件与样式(11)-等待/忙/正在加载状态-控件实现
WPF自定义控件与样式(12)-缩略图ThumbnailImage /gif动画图/图片列表
WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox
WPF自定义控件与样式(14)-轻量MVVM模式实践
WPF自定义控件与样式(15)-终结篇
一.总结.声明
关于本WPF,本人也只能算是一个刚入门的,在学习中和工作中,学习、借鉴了很多网友的文章和开源的项目的知识。发现提供实际项目开发需要的基础控件、样式的文章大都比较散,不成系列。因此基于现在项目中使用的基础UI库,写了这个系列,希望对有需要的人有所帮助。
本系列包括本文共15篇,就到此为止了。关于一些问题再次说明一下:
二.附录.本系列补充资源
2.1附加属性
很多样式中使用了附加属性来对基础控件扩展,ControlAttachProperty.cs所有代码:
公共附加属性 ControlAttachProperty { FocusBackground 获得焦点背景色, DependencyProperty FocusBackgroundProperty = DependencyProperty.RegisterAttached( , typeof(Brush), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null)); SetFocusBackground(DependencyObject element, Brush value) { element.SetValue(FocusBackgroundProperty, value); } public static Brush GetFocusBackground(DependencyObject element) { return (Brush)element.GetValue(FocusBackgroundProperty); } #endregion #region FocusForeground 获得焦点前景色, DependencyProperty FocusForegroundProperty = DependencyProperty.RegisterAttached( , typeof(Brush), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null)); SetFocusForeground(DependencyObject element, Brush value) { element.SetValue(FocusForegroundProperty, value); } public static Brush GetFocusForeground(DependencyObject element) { return (Brush)element.GetValue(FocusForegroundProperty); } #endregion #region MouseOverBackgroundProperty 鼠标悬浮背景色 DependencyProperty MouseOverBackgroundProperty = DependencyProperty.RegisterAttached( , typeof(Brush), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null)); SetMouseOverBackground(DependencyObject element, Brush value) { element.SetValue(MouseOverBackgroundProperty, value); } public static Brush MouseOverBackground(DependencyObject element) { return (Brush)element.GetValue(FocusBackgroundProperty); } #endregion #region MouseOverForegroundProperty 鼠标悬浮前景色 DependencyProperty MouseOverForegroundProperty = DependencyProperty.RegisterAttached( , typeof(Brush), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null)); SetMouseOverForeground(DependencyObject element, Brush value) { element.SetValue(MouseOverForegroundProperty, value); } public static Brush MouseOverForeground(DependencyObject element) { return (Brush)element.GetValue(FocusForegroundProperty); } #endregion #region FocusBorderBrush 焦点边框色,输入控件 DependencyProperty FocusBorderBrushProperty = DependencyProperty.RegisterAttached( , typeof(Brush), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null)); SetFocusBorderBrush(DependencyObject element, Brush value) { element.SetValue(FocusBorderBrushProperty, value); } public static Brush GetFocusBorderBrush(DependencyObject element) { return (Brush)element.GetValue(FocusBorderBrushProperty); } #endregion #region MouseOverBorderBrush 鼠标进入边框色,输入控件 DependencyProperty MouseOverBorderBrushProperty = DependencyProperty.RegisterAttached(, typeof(Brush), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits)); Sets the brush used to draw the mouse over brush. SetMouseOverBorderBrush(DependencyObject obj, Brush value) { obj.SetValue(MouseOverBorderBrushProperty, value); } Gets the brush used to draw the mouse over brush. /// </summary> [AttachedPropertyBrowsableForType(typeof(TextBox))] [AttachedPropertyBrowsableForType(typeof(CheckBox))] [AttachedPropertyBrowsableForType(typeof(RadioButton))] [AttachedPropertyBrowsableForType(typeof(DatePicker))] [AttachedPropertyBrowsableForType(typeof(ComboBox))] [AttachedPropertyBrowsableForType(typeof(RichTextBox))] public static Brush GetMouseOverBorderBrush(DependencyObject obj) { return (Brush)obj.GetValue(MouseOverBorderBrushProperty); } #endregion #region AttachContentProperty 附加组件模板 附加组件模板 DependencyProperty AttachContentProperty = DependencyProperty.RegisterAttached( , typeof(ControlTemplate), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null)); public static ControlTemplate GetAttachContent(DependencyObject d) { return (ControlTemplate)d.GetValue(AttachContentProperty); } SetAttachContent(DependencyObject obj, ControlTemplate value) { obj.SetValue(AttachContentProperty, value); } #endregion #region WatermarkProperty 水印 水印 DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached( , typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata("")); GetWatermark(DependencyObject d) { return (string)d.GetValue(WatermarkProperty); } SetWatermark(DependencyObject obj, string value) { obj.SetValue(WatermarkProperty, value); } #endregion #region FIconProperty 字体图标 字体图标 DependencyProperty FIconProperty = DependencyProperty.RegisterAttached( , typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata("")); GetFIcon(DependencyObject d) { return (string)d.GetValue(FIconProperty); } SetFIcon(DependencyObject obj, string value) { obj.SetValue(FIconProperty, value); } #endregion #region FIconSizeProperty 字体图标大小 字体图标 DependencyProperty FIconSizeProperty = DependencyProperty.RegisterAttached( , typeof(double), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(12D)); GetFIconSize(DependencyObject d) { return (double)d.GetValue(FIconSizeProperty); } SetFIconSize(DependencyObject obj, double value) { obj.SetValue(FIconSizeProperty, value); } #endregion #region FIconMarginProperty 字体图标边距 字体图标 DependencyProperty FIconMarginProperty = DependencyProperty.RegisterAttached( , typeof(Thickness), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null)); public static Thickness GetFIconMargin(DependencyObject d) { return (Thickness)d.GetValue(FIconMarginProperty); } SetFIconMargin(DependencyObject obj, Thickness value) { obj.SetValue(FIconMarginProperty, value); } #endregion #region AllowsAnimationProperty 启用旋转动画 启用旋转动画 DependencyProperty AllowsAnimationProperty = DependencyProperty.RegisterAttached( , typeof(bool), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(false, AllowsAnimationChanged)); GetAllowsAnimation(DependencyObject d) { return (bool)d.GetValue(AllowsAnimationProperty); } SetAllowsAnimation(DependencyObject obj, bool value) { obj.SetValue(AllowsAnimationProperty, value); } 旋转动画刻度 DoubleAnimation RotateAnimation = new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(200))); 绑定动画事件 AllowsAnimationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var uc = d as FrameworkElement; if (uc == null) return; if (uc.RenderTransformOrigin == new Point(0, 0)) { uc.RenderTransformOrigin = new Point(0.5, 0.5); RotateTransform trans = new RotateTransform(0); uc.RenderTransform = trans; } var value = (bool)e.NewValue; if (value) { RotateAnimation.To = 180; uc.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, RotateAnimation); } else { RotateAnimation.To = 0; uc.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, RotateAnimation); } } #endregion #region CornerRadiusProperty Border圆角 Border圆角 DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached( , typeof(CornerRadius), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null)); public static CornerRadius GetCornerRadius(DependencyObject d) { return (CornerRadius)d.GetValue(CornerRadiusProperty); } SetCornerRadius(DependencyObject obj, CornerRadius value) { obj.SetValue(CornerRadiusProperty, value); } #endregion #region LabelProperty TextBox的头部Label TextBox的头部Label DependencyProperty LabelProperty = DependencyProperty.RegisterAttached( , typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null)); [AttachedPropertyBrowsableForType(typeof(TextBox))] GetLabel(DependencyObject d) { return (string)d.GetValue(LabelProperty); } SetLabel(DependencyObject obj, string value) { obj.SetValue(LabelProperty, value); } #endregion #region LabelTemplateProperty TextBox的头部Label模板 TextBox的头部Label模板 DependencyProperty LabelTemplateProperty = DependencyProperty.RegisterAttached( , typeof(ControlTemplate), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null)); [AttachedPropertyBrowsableForType(typeof(TextBox))] public static ControlTemplate GetLabelTemplate(DependencyObject d) { return (ControlTemplate)d.GetValue(LabelTemplateProperty); } SetLabelTemplate(DependencyObject obj, ControlTemplate value) { obj.SetValue(LabelTemplateProperty, value); } IsClearTextButtonBehaviorEnabledProperty 清除输入框Text值按钮行为开关(设为ture时才会绑定事件) 清除输入框Text值按钮行为开关 DependencyProperty IsClearTextButtonBehaviorEnabledProperty = DependencyProperty.RegisterAttached( , typeof(bool), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(false, IsClearTextButtonBehaviorEnabledChanged)); [AttachedPropertyBrowsableForType(typeof(TextBox))] GetIsClearTextButtonBehaviorEnabled(DependencyObject d) { return (bool)d.GetValue(IsClearTextButtonBehaviorEnabledProperty); } SetIsClearTextButtonBehaviorEnabled(DependencyObject obj, bool value) { obj.SetValue(IsClearTextButtonBehaviorEnabledProperty, value); } 绑定清除Text操作的按钮事件 IsClearTextButtonBehaviorEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var button = d as FButton; if (e.OldValue != e.NewValue && button != null) { button.CommandBindings.Add(ClearTextCommandBinding); } } #endregion #region IsOpenFileButtonBehaviorEnabledProperty 选择文件命令行为开关 选择文件命令行为开关 DependencyProperty IsOpenFileButtonBehaviorEnabledProperty = DependencyProperty.RegisterAttached( , typeof(bool), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(false, IsOpenFileButtonBehaviorEnabledChanged)); [AttachedPropertyBrowsableForType(typeof(TextBox))] GetIsOpenFileButtonBehaviorEnabled(DependencyObject d) { return (bool)d.GetValue(IsOpenFileButtonBehaviorEnabledProperty); } SetIsOpenFileButtonBehaviorEnabled(DependencyObject obj, bool value) { obj.SetValue(IsOpenFileButtonBehaviorEnabledProperty, value); } IsOpenFileButtonBehaviorEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var button = d as FButton; if (e.OldValue != e.NewValue && button != null) { button.CommandBindings.Add(OpenFileCommandBinding); } } #endregion #region IsOpenFolderButtonBehaviorEnabledProperty 选择文件夹命令行为开关 选择文件夹命令行为开关 DependencyProperty IsOpenFolderButtonBehaviorEnabledProperty = DependencyProperty.RegisterAttached( , typeof(bool), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(false, IsOpenFolderButtonBehaviorEnabledChanged)); [AttachedPropertyBrowsableForType(typeof(TextBox))] GetIsOpenFolderButtonBehaviorEnabled(DependencyObject d) { return (bool)d.GetValue(IsOpenFolderButtonBehaviorEnabledProperty); } SetIsOpenFolderButtonBehaviorEnabled(DependencyObject obj, bool value) { obj.SetValue(IsOpenFolderButtonBehaviorEnabledProperty, value); } IsOpenFolderButtonBehaviorEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var button = d as FButton; if (e.OldValue != e.NewValue && button != null) { button.CommandBindings.Add(OpenFolderCommandBinding); } } #endregion #region IsSaveFileButtonBehaviorEnabledProperty 选择文件保存路径及名称 选择文件保存路径及名称 DependencyProperty IsSaveFileButtonBehaviorEnabledProperty = DependencyProperty.RegisterAttached( , typeof(bool), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(false, IsSaveFileButtonBehaviorEnabledChanged)); [AttachedPropertyBrowsableForType(typeof(TextBox))] GetIsSaveFileButtonBehaviorEnabled(DependencyObject d) { return (bool)d.GetValue(IsSaveFileButtonBehaviorEnabledProperty); } SetIsSaveFileButtonBehaviorEnabled(DependencyObject obj, bool value) { obj.SetValue(IsSaveFileButtonBehaviorEnabledProperty, value); } IsSaveFileButtonBehaviorEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var button = d as FButton; if (e.OldValue != e.NewValue && button != null) { button.CommandBindings.Add(SaveFileCommandBinding); } } ClearTextCommand 清除输入框Text事件命令 清除输入框Text事件命令,需要使用IsClearTextButtonBehaviorEnabledChanged绑定命令 RoutedUICommand ClearTextCommand { get; private set; } ClearTextCommand绑定事件 CommandBinding ClearTextCommandBinding; 清除输入框文本值 ClearButtonClick(object sender, ExecutedRoutedEventArgs e) { var tbox = e.Parameter as FrameworkElement; if (tbox == null) return; if (tbox is TextBox) { ((TextBox)tbox).Clear(); } if (tbox is PasswordBox) { ((PasswordBox)tbox).Clear(); } if (tbox is ComboBox) { var cb = tbox as ComboBox; cb.SelectedItem = null; cb.Text = string.Empty; } if (tbox is MultiComboBox) { var cb = tbox as MultiComboBox; cb.SelectedItem = null; cb.UnselectAll(); cb.Text = string.Empty; } if (tbox is DatePicker) { var dp = tbox as DatePicker; dp.SelectedDate = null; dp.Text = string.Empty; } tbox.Focus(); } #endregion #region OpenFileCommand 选择文件命令 选择文件命令,需要使用IsClearTextButtonBehaviorEnabledChanged绑定命令 RoutedUICommand OpenFileCommand { get; private set; } OpenFileCommand绑定事件 CommandBinding OpenFileCommandBinding; 执行OpenFileCommand OpenFileButtonClick(object sender, ExecutedRoutedEventArgs e) { var tbox = e.Parameter as FrameworkElement; var txt = tbox as TextBox; : txt.Tag.ToString(); )) { filter += ; } if (txt == null) return; OpenFileDialog fd = new OpenFileDialog(); fd.Title = ; //“图像文件(*.bmp, *.jpg)|*.bmp;*.jpg|所有文件(*.*)|*.*” fd.Filter = filter; fd.FileName = txt.Text.Trim(); if (fd.ShowDialog() == true) { txt.Text = fd.FileName; } tbox.Focus(); } #endregion #region OpenFolderCommand 选择文件夹命令 选择文件夹命令 RoutedUICommand OpenFolderCommand { get; private set; } OpenFolderCommand绑定事件 CommandBinding OpenFolderCommandBinding; 执行OpenFolderCommand OpenFolderButtonClick(object sender, ExecutedRoutedEventArgs e) { var tbox = e.Parameter as FrameworkElement; var txt = tbox as TextBox; if (txt == null) return; FolderBrowserDialog fd = new FolderBrowserDialog(); fd.Description = ; fd.SelectedPath = txt.Text.Trim(); if (fd.ShowDialog() == DialogResult.OK) { txt.Text = fd.SelectedPath; } tbox.Focus(); } #endregion #region SaveFileCommand 选择文件保存路径及名称 选择文件保存路径及名称 RoutedUICommand SaveFileCommand { get; private set; } SaveFileCommand绑定事件 CommandBinding SaveFileCommandBinding; 执行OpenFileCommand SaveFileButtonClick(object sender, ExecutedRoutedEventArgs e) { var tbox = e.Parameter as FrameworkElement; var txt = tbox as TextBox; if (txt == null) return; SaveFileDialog fd = new SaveFileDialog(); fd.Title = ; fd.Filter = ; fd.FileName = txt.Text.Trim(); if (fd.ShowDialog() == DialogResult.OK) { txt.Text = fd.FileName; } tbox.Focus(); } 静态构造函数 ControlAttachProperty() { //ClearTextCommand ClearTextCommand = new RoutedUICommand(); ClearTextCommandBinding = new CommandBinding(ClearTextCommand); ClearTextCommandBinding.Executed += ClearButtonClick; //OpenFileCommand OpenFileCommand = new RoutedUICommand(); OpenFileCommandBinding = new CommandBinding(OpenFileCommand); OpenFileCommandBinding.Executed += OpenFileButtonClick; //OpenFolderCommand OpenFolderCommand = new RoutedUICommand(); OpenFolderCommandBinding = new CommandBinding(OpenFolderCommand); OpenFolderCommandBinding.Executed += OpenFolderButtonClick; SaveFileCommand = new RoutedUICommand(); SaveFileCommandBinding = new CommandBinding(SaveFileCommand); SaveFileCommandBinding.Executed += SaveFileButtonClick; } }
View Code