当前位置: 首页 > news >正文

seo网站优化价格小程

seo网站优化价格,小程,wordpress取消评论,网站广告推送怎么做一#xff0e;前言.预览 申明#xff1a;WPF自定义控件与样式是一个系列文章#xff0c;前后是有些关联的#xff0c;但大多是按照由简到繁的顺序逐步发布的等。 本文主要是对文本输入控件进行样式开发#xff0c;及相关扩展功能开发#xff0c;主要内容包括#xff1a;…一前言.预览   申明WPF自定义控件与样式是一个系列文章前后是有些关联的但大多是按照由简到繁的顺序逐步发布的等。 本文主要是对文本输入控件进行样式开发及相关扩展功能开发主要内容包括 基本文本框TextBox控件样式及扩展功能实现了样式、水印、Label标签、功能扩展富文本框RichTextBox控件样式密码输入框PasswordBox控件样式及扩展功能效果图 二基本文本框TextBox控件样式及扩展功能 2.1 TextBox基本样式 样式代码如下   !--TextBox默认样式--Style TargetType{x:Type TextBox} x:KeyDefaultTextBoxSetter PropertyContextMenu Value{DynamicResource TextBoxContextMenu} /Setter PropertySelectionBrush Value{StaticResource TextSelectionBrush} /Setter PropertyFontFamily Value{StaticResource FontFamily} /Setter PropertyFontSize Value{StaticResource FontSize} /Setter PropertyBorderThickness Value1 /Setter PropertyMinHeight Value26 /Setter PropertyWidth Value100 /Setter PropertyBackground Value{StaticResource TextBackground} /Setter PropertyForeground Value{StaticResource TextForeground} /Setter PropertyPadding Value0 /Setter PropertyBorderBrush Value{StaticResource ControlBorderBrush} /Setter Propertylocal:ControlAttachProperty.FocusBorderBrush Value{StaticResource FocusBorderBrush} /Setter Propertylocal:ControlAttachProperty.MouseOverBorderBrush Value{StaticResource MouseOverBorderBrush} /Setter PropertyVerticalContentAlignment ValueCenter /!-- change SnapsToDevicePixels to True to view a better border and validation error --Setter PropertySnapsToDevicePixels ValueTrue /!--英 [kærət] 美 [kærət] 插入符号--Setter PropertyCaretBrush Value{StaticResource TextForeground} /Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type TextBox}Grid x:NamePART_RootBorder x:NameBg SnapsToDevicePixels{TemplateBinding SnapsToDevicePixels}CornerRadius{TemplateBinding local:ControlAttachProperty.CornerRadius}BorderBrush{TemplateBinding BorderBrush} BorderThickness{TemplateBinding BorderThickness} Background{TemplateBinding Background} /Grid x:NamePART_InnerGridGrid.ColumnDefinitionsColumnDefinition WidthAuto /ColumnDefinition Width* /ColumnDefinition WidthAuto //Grid.ColumnDefinitions!--Label区域--ContentControl x:NameLabel Margin1 Template{TemplateBinding local:ControlAttachProperty.LabelTemplate}Content{TemplateBinding local:ControlAttachProperty.Label}/!--内容区域--ScrollViewer x:NamePART_ContentHost BorderThickness0 Grid.Column1 IsTabStopFalse Margin2VerticalAlignmentStretch Background{x:Null} /!--水印--TextBlock x:NameMessage Padding{TemplateBinding Padding} VisibilityCollapsedText{TemplateBinding local:ControlAttachProperty.Watermark} Grid.Column1Foreground{TemplateBinding Foreground} IsHitTestVisibleFalse Opacity{StaticResource WatermarkOpacity}HorizontalAlignment{TemplateBinding HorizontalContentAlignment}VerticalAlignment{TemplateBinding VerticalContentAlignment} Margin5,2,5,2 /!--附加内容区域--Border x:NamePART_AttachContent Grid.Column2 Margin2 VerticalAlignmentCenter HorizontalAlignmentCenter ContentControl VerticalAlignmentCenter VerticalContentAlignmentCenter Template{TemplateBinding local:ControlAttachProperty.AttachContent} //Border/Grid/GridControlTemplate.Triggers!--显示水印--DataTrigger Binding{Binding RelativeSource{RelativeSource Self}, PathText} ValueSetter TargetNameMessage PropertyVisibility ValueVisible //DataTriggerTrigger PropertyIsMouseOver ValueTrueSetter PropertyBorderBrush Value{Binding Path(local:ControlAttachProperty.MouseOverBorderBrush),RelativeSource{RelativeSource Self}}//TriggerTrigger PropertyIsFocused ValueTrueSetter PropertyBorderBrush Value{Binding Path(local:ControlAttachProperty.FocusBorderBrush),RelativeSource{RelativeSource Self}}//Trigger!--不可用--Trigger PropertyIsEnabled ValueFalseSetter TargetNamePART_Root PropertyOpacity Value{StaticResource DisableOpacity} //Trigger!--只读时禁用PART_AttachContent--Trigger PropertyIsReadOnly ValueTrueSetter TargetNamePART_AttachContent PropertyIsEnabled ValueFalse /Setter TargetNameBg PropertyOpacity Value{StaticResource ReadonlyOpacity} /Setter TargetNamePART_ContentHost PropertyOpacity Value{StaticResource ReadonlyOpacity} /Setter TargetNameLabel PropertyOpacity Value{StaticResource ReadonlyOpacity} //Trigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/Setter/Style 模板内容主要包含四部分 用于实现Label标签的预留区域TextBox本身的文本输入显示部分水印显示部分功能扩展的预留区域  其中Label标签、功能扩展还有输入框的不同状态显示效果都是通过附加属性来实现的其实从本质上附加属性和控件上定义的依赖属性是同一个概念有些时候附加属性会更加方便对于一些可共用的属性就比较方便这一点怎本文是有体现的。上面代码使用到的附加属性代码 #region FocusBorderBrush 焦点边框色输入控件public static readonly DependencyProperty FocusBorderBrushProperty DependencyProperty.RegisterAttached(FocusBorderBrush, typeof(Brush), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));public static void SetFocusBorderBrush(DependencyObject element, Brush value){element.SetValue(FocusBorderBrushProperty, value);}public static Brush GetFocusBorderBrush(DependencyObject element){return (Brush)element.GetValue(FocusBorderBrushProperty);}#endregion#region MouseOverBorderBrush 鼠标进入边框色输入控件public static readonly DependencyProperty MouseOverBorderBrushProperty DependencyProperty.RegisterAttached(MouseOverBorderBrush, typeof(Brush), typeof(ControlAttachProperty),new FrameworkPropertyMetadata(Brushes.Transparent,FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits));/// summary/// Sets the brush used to draw the mouse over brush./// /summarypublic static void SetMouseOverBorderBrush(DependencyObject obj, Brush value){obj.SetValue(MouseOverBorderBrushProperty, value);}/// summary/// 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 附加组件模板/// summary/// 附加组件模板/// /summarypublic static readonly DependencyProperty AttachContentProperty DependencyProperty.RegisterAttached(AttachContent, typeof(ControlTemplate), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));public static ControlTemplate GetAttachContent(DependencyObject d){return (ControlTemplate)d.GetValue(AttachContentProperty);}public static void SetAttachContent(DependencyObject obj, ControlTemplate value){obj.SetValue(AttachContentProperty, value);}#endregion#region WatermarkProperty 水印/// summary/// 水印/// /summarypublic static readonly DependencyProperty WatermarkProperty DependencyProperty.RegisterAttached(Watermark, typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata());public static string GetWatermark(DependencyObject d){return (string)d.GetValue(WatermarkProperty);}public static void SetWatermark(DependencyObject obj, string value){obj.SetValue(WatermarkProperty, value);}#endregion#region CornerRadiusProperty Border圆角/// summary/// Border圆角/// /summarypublic static readonly DependencyProperty CornerRadiusProperty DependencyProperty.RegisterAttached(CornerRadius, typeof(CornerRadius), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));public static CornerRadius GetCornerRadius(DependencyObject d){return (CornerRadius)d.GetValue(CornerRadiusProperty);}public static void SetCornerRadius(DependencyObject obj, CornerRadius value){obj.SetValue(CornerRadiusProperty, value);}#endregion#region LabelProperty TextBox的头部Label/// summary/// TextBox的头部Label/// /summarypublic static readonly DependencyProperty LabelProperty DependencyProperty.RegisterAttached(Label, typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));[AttachedPropertyBrowsableForType(typeof(TextBox))]public static string GetLabel(DependencyObject d){return (string)d.GetValue(LabelProperty);}public static void SetLabel(DependencyObject obj, string value){obj.SetValue(LabelProperty, value);}#endregion#region LabelTemplateProperty TextBox的头部Label模板/// summary/// TextBox的头部Label模板/// /summarypublic static readonly DependencyProperty LabelTemplateProperty DependencyProperty.RegisterAttached(LabelTemplate, typeof(ControlTemplate), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));[AttachedPropertyBrowsableForType(typeof(TextBox))]public static ControlTemplate GetLabelTemplate(DependencyObject d){return (ControlTemplate)d.GetValue(LabelTemplateProperty);}public static void SetLabelTemplate(DependencyObject obj, ControlTemplate value){obj.SetValue(LabelTemplateProperty, value);}#endregion 2.2 水印效果实现   通过2.1的代码示例可以看出水印是内置了一个TextBlock用附加属性ControlAttachProperty.Watermark设置水印内容在触发器中检测当TextBox中有输入值则隐藏水印的TextBlock使用示例   StackPanelTextBox Width140 Height40 Margin3 TextWrappingWrap VerticalScrollBarVisibilityVisible333333333333333/TextBoxTextBox Width150 Height30 Margin3 core:ControlAttachProperty.Watermark我是水印 core:ControlAttachProperty.CornerRadius2/TextBoxTextBox Width150 Height30 Margin3 IsReadOnlyTrue core:ControlAttachProperty.CornerRadius15 SnapsToDevicePixelsTrue 我是只读的/TextBoxTextBox Width150 Height30 Margin3 IsEnabledFalseIsEnabledFalse/TextBoxTextBox Width150 Height30 core:ControlAttachProperty.Watermark我是水印/TextBox/StackPanel  效果    2.3 Label标签实现   参考2.1的代码预留了Label的区域通过设置附加属性local:ControlAttachProperty.Label设置标签文本local:ControlAttachProperty.LabelTemplate设置Label标签的模板样式即可自定义实现Label标签自定义样式 !--TextBox包含附加属性Label的样式--Style TargetType{x:Type TextBox} x:KeyLabelTextBox BasedOn{StaticResource DefaultTextBox}Setter Propertylocal:ControlAttachProperty.LabelTemplate Setter.ValueControlTemplate TargetTypeContentControlBorder Width60 Background{StaticResource TextLabelBackground}TextBlock VerticalAlignmentCenter HorizontalAlignmentRight Margin3 Text{TemplateBinding Content}/TextBlock/Border/ControlTemplate/Setter.Value/Setter/Style 使用示例及效果  TextBox Width200 Height30 Margin3 core:ControlAttachProperty.Watermark请输入姓名 Style{StaticResource LabelTextBox} core:ControlAttachProperty.Label姓名/TextBox 2.4 扩展功能及自定义扩展   思路和2.3的Label标签实现相似清除文本框内的内容是一个常用需求我们就线扩展一个这么一个功能的TextBox通过附加属性ControlAttachProperty.AttachContent定义扩展功能的模板模板内定义的是一个按钮FButton可参考上一篇本文末尾附录中有链接   !--TextBox包含清除Text按钮的样式--Style TargetType{x:Type TextBox} x:KeyClearButtonTextBox BasedOn{StaticResource DefaultTextBox}Setter Propertylocal:ControlAttachProperty.AttachContentSetter.ValueControlTemplatelocal:FButton FIcon#xe60a; Style{StaticResource FButton_Transparency} IsTabStopFalse FIconMargin0local:ControlAttachProperty.IsClearTextButtonBehaviorEnabledTrue Commandlocal:ControlAttachProperty.ClearTextCommand CommandParameter{Binding RelativeSource{RelativeSource FindAncestor,AncestorType{x:Type TextBox}}}Margin1,3,1,4 FIconSize14 Foreground{StaticResource TextForeground} CursorArrow//ControlTemplate/Setter.Value/Setter/Style 这里定义的是显示效果清除TextBox内容的逻辑代码如何实现的呢还是附加属性 ControlAttachProperty.IsClearTextButtonBehaviorEnabledTrue 注入事件到当前ButtonCommandlocal:ControlAttachProperty.ClearTextCommand定义Fbutton的命令对象实例CommandCommandParameter{Binding RelativeSource{RelativeSource FindAncestor,AncestorType{x:Type TextBox}}}把TextBox作为参数传入  逻辑代码如下从代码不难看出它是支持多种输入控件的内容清除的也就是说该扩展功能可以轻松支持其他输入控件第四节密码数据的清除也是这样使用的。 #region IsClearTextButtonBehaviorEnabledProperty 清除输入框Text值按钮行为开关设为ture时才会绑定事件/// summary/// 清除输入框Text值按钮行为开关/// /summarypublic static readonly DependencyProperty IsClearTextButtonBehaviorEnabledProperty DependencyProperty.RegisterAttached(IsClearTextButtonBehaviorEnabled, typeof(bool), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(false, IsClearTextButtonBehaviorEnabledChanged));[AttachedPropertyBrowsableForType(typeof(TextBox))]public static bool GetIsClearTextButtonBehaviorEnabled(DependencyObject d){return (bool)d.GetValue(IsClearTextButtonBehaviorEnabledProperty);}public static void SetIsClearTextButtonBehaviorEnabled(DependencyObject obj, bool value){obj.SetValue(IsClearTextButtonBehaviorEnabledProperty, value);}/// summary/// 绑定清除Text操作的按钮事件/// /summaryprivate static void IsClearTextButtonBehaviorEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){var button d as FButton;if (e.OldValue ! e.NewValue button ! null){button.CommandBindings.Add(ClearTextCommandBinding);}}#endregion#region ClearTextCommand 清除输入框Text事件命令/// summary/// 清除输入框Text事件命令需要使用IsClearTextButtonBehaviorEnabledChanged绑定命令/// /summarypublic static RoutedUICommand ClearTextCommand { get; private set; }/// summary/// ClearTextCommand绑定事件/// /summaryprivate static readonly CommandBinding ClearTextCommandBinding;/// summary/// 清除输入框文本值/// /summaryprivate static void 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/// summary/// 静态构造函数/// /summarystatic ControlAttachProperty(){//ClearTextCommandClearTextCommand new RoutedUICommand();ClearTextCommandBinding new CommandBinding(ClearTextCommand);ClearTextCommandBinding.Executed ClearButtonClick;//OpenFileCommandOpenFileCommand new RoutedUICommand();OpenFileCommandBinding new CommandBinding(OpenFileCommand);OpenFileCommandBinding.Executed OpenFileButtonClick;//OpenFolderCommandOpenFolderCommand new RoutedUICommand();OpenFolderCommandBinding new CommandBinding(OpenFolderCommand);OpenFolderCommandBinding.Executed OpenFolderButtonClick;SaveFileCommand new RoutedUICommand();SaveFileCommandBinding new CommandBinding(SaveFileCommand);SaveFileCommandBinding.Executed SaveFileButtonClick;} 效果    当然我们也可以自定义扩展其他功能如   TextBox Width200 Height30 Margin3 core:ControlAttachProperty.Watermark查询关键词 IsEnabledTruecore:ControlAttachProperty.AttachContentControlTemplateStackPanel OrientationHorizontalcore:FButton FIcon#xe60b; Style{StaticResource FButton_Transparency} IsTabStopFalse FIconMargin0FIconSize18 Margin1,1,2,3 Foreground{StaticResource TextForeground} CursorArrow/core:FButton FIcon#xe628; Style{StaticResource FButton_Transparency} IsTabStopFalse FIconMargin0FIconSize22 Foreground{StaticResource TextForeground} CursorArrow//StackPanel/ControlTemplate/core:ControlAttachProperty.AttachContent/TextBox 效果 由上不难同时实现Label标签和清除文本内容的样式 !--TextBox包含附加属性Label以及ClearText按钮的样式--Style TargetType{x:Type TextBox} x:KeyLabelClearButtonTextBox BasedOn{StaticResource DefaultTextBox}Setter Propertylocal:ControlAttachProperty.LabelTemplate Setter.ValueControlTemplate TargetTypeContentControlBorder Width60 Background{StaticResource TextLabelBackground}TextBlock VerticalAlignmentCenter HorizontalAlignmentRight Margin3 Text{TemplateBinding Content}/TextBlock/Border/ControlTemplate/Setter.Value/SetterSetter Propertylocal:ControlAttachProperty.AttachContentSetter.ValueControlTemplatelocal:FButton FIcon#xe60a; Style{StaticResource FButton_Transparency} IsTabStopFalse FIconMargin0local:ControlAttachProperty.IsClearTextButtonBehaviorEnabledTrue Commandlocal:ControlAttachProperty.ClearTextCommand CommandParameter{Binding RelativeSource{RelativeSource FindAncestor,AncestorType{x:Type TextBox}}}Margin0,3,1,4 FIconSize14 Foreground{StaticResource TextForeground} CursorArrow//ControlTemplate/Setter.Value/Setter/Style 2.6 文件选择输入相关扩展   先看看效果就明白了。     具体实现原理和上面2.4差不多 实现了三个文件、文件夹选择相关的功能扩展样式代码 !--LabelOpenFileTextBox--Style TargetType{x:Type TextBox} x:KeyLabelOpenFileTextBox BasedOn{StaticResource LabelClearButtonTextBox}Setter Propertylocal:ControlAttachProperty.Label Value文件路径/Setter Propertylocal:ControlAttachProperty.Watermark Value选择文件路径/Setter Propertylocal:ControlAttachProperty.AttachContentSetter.ValueControlTemplatelocal:FButton FIcon#xe64e; Style{StaticResource FButton_Transparency} IsTabStopFalse FIconMargin0local:ControlAttachProperty.IsOpenFileButtonBehaviorEnabledTrue Commandlocal:ControlAttachProperty.OpenFileCommand CommandParameter{Binding RelativeSource{RelativeSource FindAncestor,AncestorType{x:Type TextBox}}}Margin0,1,0,1 FIconSize22 Foreground{StaticResource TextForeground} CursorArrow//ControlTemplate/Setter.Value/Setter/Style!--LabelOpenFolderTextBox--Style TargetType{x:Type TextBox} x:KeyLabelOpenFolderTextBox BasedOn{StaticResource LabelClearButtonTextBox}Setter Propertylocal:ControlAttachProperty.Label Value设置路径/Setter Propertylocal:ControlAttachProperty.Watermark Value选择文件夹路径/Setter Propertylocal:ControlAttachProperty.AttachContentSetter.ValueControlTemplatelocal:FButton FIcon#xe636; Style{StaticResource FButton_Transparency} IsTabStopFalse FIconMargin0local:ControlAttachProperty.IsOpenFolderButtonBehaviorEnabledTrue Commandlocal:ControlAttachProperty.OpenFolderCommand CommandParameter{Binding RelativeSource{RelativeSource FindAncestor,AncestorType{x:Type TextBox}}}Margin0,1,0,1 FIconSize22 Foreground{StaticResource TextForeground} CursorArrow//ControlTemplate/Setter.Value/Setter/Style!--LabelSaveFileTextBox--Style TargetType{x:Type TextBox} x:KeyLabelSaveFileTextBox BasedOn{StaticResource LabelClearButtonTextBox}Setter Propertylocal:ControlAttachProperty.Label Value保存路径/Setter Propertylocal:ControlAttachProperty.Watermark Value选择文件保存路径/Setter Propertylocal:ControlAttachProperty.AttachContentSetter.ValueControlTemplatelocal:FButton FIcon#xe61a; Style{StaticResource FButton_Transparency} IsTabStopFalse FIconMargin0local:ControlAttachProperty.IsSaveFileButtonBehaviorEnabledTrue Commandlocal:ControlAttachProperty.SaveFileCommand CommandParameter{Binding RelativeSource{RelativeSource FindAncestor,AncestorType{x:Type TextBox}}}Margin0,1,0,1 FIconSize20 Foreground{StaticResource TextForeground} CursorArrow//ControlTemplate/Setter.Value/Setter/Style 当然实现原理和2.4一样都是依赖属性来实现事件的注入和绑定的所以就不多废话了 #region IsOpenFileButtonBehaviorEnabledProperty 选择文件命令行为开关/// summary/// 选择文件命令行为开关/// /summarypublic static readonly DependencyProperty IsOpenFileButtonBehaviorEnabledProperty DependencyProperty.RegisterAttached(IsOpenFileButtonBehaviorEnabled, typeof(bool), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(false, IsOpenFileButtonBehaviorEnabledChanged));[AttachedPropertyBrowsableForType(typeof(TextBox))]public static bool GetIsOpenFileButtonBehaviorEnabled(DependencyObject d){return (bool)d.GetValue(IsOpenFileButtonBehaviorEnabledProperty);}public static void SetIsOpenFileButtonBehaviorEnabled(DependencyObject obj, bool value){obj.SetValue(IsOpenFileButtonBehaviorEnabledProperty, value);}private static void IsOpenFileButtonBehaviorEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){var button d as FButton;if (e.OldValue ! e.NewValue button ! null){button.CommandBindings.Add(OpenFileCommandBinding);}}#endregion#region IsOpenFolderButtonBehaviorEnabledProperty 选择文件夹命令行为开关/// summary/// 选择文件夹命令行为开关/// /summarypublic static readonly DependencyProperty IsOpenFolderButtonBehaviorEnabledProperty DependencyProperty.RegisterAttached(IsOpenFolderButtonBehaviorEnabled, typeof(bool), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(false, IsOpenFolderButtonBehaviorEnabledChanged));[AttachedPropertyBrowsableForType(typeof(TextBox))]public static bool GetIsOpenFolderButtonBehaviorEnabled(DependencyObject d){return (bool)d.GetValue(IsOpenFolderButtonBehaviorEnabledProperty);}public static void SetIsOpenFolderButtonBehaviorEnabled(DependencyObject obj, bool value){obj.SetValue(IsOpenFolderButtonBehaviorEnabledProperty, value);}private static void IsOpenFolderButtonBehaviorEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){var button d as FButton;if (e.OldValue ! e.NewValue button ! null){button.CommandBindings.Add(OpenFolderCommandBinding);}}#endregion#region IsSaveFileButtonBehaviorEnabledProperty 选择文件保存路径及名称/// summary/// 选择文件保存路径及名称/// /summarypublic static readonly DependencyProperty IsSaveFileButtonBehaviorEnabledProperty DependencyProperty.RegisterAttached(IsSaveFileButtonBehaviorEnabled, typeof(bool), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(false, IsSaveFileButtonBehaviorEnabledChanged));[AttachedPropertyBrowsableForType(typeof(TextBox))]public static bool GetIsSaveFileButtonBehaviorEnabled(DependencyObject d){return (bool)d.GetValue(IsSaveFileButtonBehaviorEnabledProperty);}public static void SetIsSaveFileButtonBehaviorEnabled(DependencyObject obj, bool value){obj.SetValue(IsSaveFileButtonBehaviorEnabledProperty, value);}private static void IsSaveFileButtonBehaviorEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){var button d as FButton;if (e.OldValue ! e.NewValue button ! null){button.CommandBindings.Add(SaveFileCommandBinding);}}#endregion#region OpenFileCommand 选择文件命令/// summary/// 选择文件命令需要使用IsClearTextButtonBehaviorEnabledChanged绑定命令/// /summarypublic static RoutedUICommand OpenFileCommand { get; private set; }/// summary/// OpenFileCommand绑定事件/// /summaryprivate static readonly CommandBinding OpenFileCommandBinding;/// summary/// 执行OpenFileCommand/// /summaryprivate static void OpenFileButtonClick(object sender, ExecutedRoutedEventArgs e){var tbox e.Parameter as FrameworkElement;var txt tbox as TextBox;string filter txt.Tag null ? 所有文件(*.*)|*.* : txt.Tag.ToString();if (filter.Contains(.bin)){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 选择文件夹命令/// summary/// 选择文件夹命令/// /summarypublic static RoutedUICommand OpenFolderCommand { get; private set; }/// summary/// OpenFolderCommand绑定事件/// /summaryprivate static readonly CommandBinding OpenFolderCommandBinding;/// summary/// 执行OpenFolderCommand/// /summaryprivate static void 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 选择文件保存路径及名称/// summary/// 选择文件保存路径及名称/// /summarypublic static RoutedUICommand SaveFileCommand { get; private set; }/// summary/// SaveFileCommand绑定事件/// /summaryprivate static readonly CommandBinding SaveFileCommandBinding;/// summary/// 执行OpenFileCommand/// /summaryprivate static void 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();}#endregion/// summary/// 静态构造函数/// /summarystatic ControlAttachProperty(){//ClearTextCommandClearTextCommand new RoutedUICommand();ClearTextCommandBinding new CommandBinding(ClearTextCommand);ClearTextCommandBinding.Executed ClearButtonClick;//OpenFileCommandOpenFileCommand new RoutedUICommand();OpenFileCommandBinding new CommandBinding(OpenFileCommand);OpenFileCommandBinding.Executed OpenFileButtonClick;//OpenFolderCommandOpenFolderCommand new RoutedUICommand();OpenFolderCommandBinding new CommandBinding(OpenFolderCommand);OpenFolderCommandBinding.Executed OpenFolderButtonClick;SaveFileCommand new RoutedUICommand();SaveFileCommandBinding new CommandBinding(SaveFileCommand);SaveFileCommandBinding.Executed SaveFileButtonClick;}  三富文本框RichTextBox控件样式   RichTextBox的样式比较简单   !--***************************DefaultRichTextBox***************************--Style x:KeyDefaultRichTextBox TargetType{x:Type RichTextBox}Setter PropertyContextMenu Value{DynamicResource TextBoxContextMenu} /Setter PropertySelectionBrush Value{StaticResource TextSelectionBrush} /Setter PropertyFontFamily Value{StaticResource FontFamily} /Setter PropertyFontSize Value{StaticResource FontSize} /Setter PropertyBorderThickness Value1 /Setter PropertyBorderBrush Value{StaticResource ControlBorderBrush} /Setter PropertyMinHeight Value26 /Setter PropertyMinWidth Value10 /Setter PropertyBackground Value{StaticResource TextBackground} /Setter PropertyForeground Value{StaticResource TextForeground} /Setter PropertyCaretBrush Value{StaticResource TextForeground} /Setter Propertylocal:ControlAttachProperty.FocusBorderBrush Value{StaticResource FocusBorderBrush} /Setter Propertylocal:ControlAttachProperty.MouseOverBorderBrush Value{StaticResource MouseOverBorderBrush} /Setter PropertyPadding Value1 /Setter PropertyAllowDrop ValueTrue /Setter PropertyVerticalScrollBarVisibility ValueAuto /Setter PropertyFocusVisualStyle Value{x:Null} /Setter PropertyScrollViewer.PanningMode ValueVerticalFirst /!--该值指示是否启用了笔势--Setter PropertyStylus.IsFlicksEnabled ValueFalse /!--SnapsToDevicePixels:该值来确定呈现此元素是否应使用特定于设备的像素设置--Setter PropertySnapsToDevicePixels ValueTrue /Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type TextBoxBase}GridBorder x:NameBdBorderBrush{TemplateBinding BorderBrush} BorderThickness{TemplateBinding BorderThickness}Background{TemplateBinding Background} SnapsToDevicePixels{TemplateBinding SnapsToDevicePixels}ScrollViewer x:NamePART_ContentHost SnapsToDevicePixels{TemplateBinding SnapsToDevicePixels} //Border/GridControlTemplate.TriggersTrigger PropertyIsMouseOver ValueTrueSetter PropertyBorderBrush Value{Binding Path(local:ControlAttachProperty.MouseOverBorderBrush),RelativeSource{RelativeSource Self}}//TriggerTrigger PropertyIsFocused ValueTrueSetter PropertyBorderBrush Value{Binding Path(local:ControlAttachProperty.FocusBorderBrush),RelativeSource{RelativeSource Self}}//TriggerTrigger PropertyIsEnabled ValueFalseSetter TargetNameBd PropertyOpacity Value0.5 //TriggerTrigger PropertyIsReadOnly ValueTrueSetter TargetNameBd PropertyOpacity Value0.85 //Trigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/Setter/Style 使用实力及效果   四密码输入框PasswordBox控件样式及扩展功能   密码输入控件的样式和第二节文本框TextBox基本一致就不做详细的说明了直接上样式的代码相关逻辑C# 代码和上面是一样的复用。 !--TextBox默认样式--Style TargetType{x:Type PasswordBox} x:KeyDefaultPasswordBoxSetter PropertyContextMenu Value{DynamicResource TextBoxContextMenu} /Setter PropertySelectionBrush Value{StaticResource TextSelectionBrush} /Setter PropertyFontFamily Value{StaticResource FontFamily} /Setter PropertyFontSize Value{StaticResource FontSize} /Setter PropertyBorderThickness Value1 /Setter PropertyPasswordChar Value●/Setter PropertyHeight Value30 /Setter PropertyWidth Value200 /Setter PropertyBackground Value{StaticResource TextBackground} /Setter PropertyForeground Value{StaticResource TextForeground} /Setter PropertyPadding Value0 /Setter PropertyBorderBrush Value{StaticResource ControlBorderBrush} /Setter Propertylocal:ControlAttachProperty.FocusBorderBrush Value{StaticResource FocusBorderBrush} /Setter Propertylocal:ControlAttachProperty.MouseOverBorderBrush Value{StaticResource MouseOverBorderBrush} /Setter PropertyVerticalContentAlignment ValueCenter /!-- change SnapsToDevicePixels to True to view a better border and validation error --Setter PropertySnapsToDevicePixels ValueTrue /!--英 [kærət] 美 [kærət] 插入符号--Setter PropertyCaretBrush Value{StaticResource TextForeground} /Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type PasswordBox}Grid x:NamePART_RootBorder x:NameBg SnapsToDevicePixels{TemplateBinding SnapsToDevicePixels}CornerRadius{TemplateBinding local:ControlAttachProperty.CornerRadius}BorderBrush{TemplateBinding BorderBrush} BorderThickness{TemplateBinding BorderThickness} Background{TemplateBinding Background} /Grid x:NamePART_InnerGridGrid.ColumnDefinitionsColumnDefinition WidthAuto /ColumnDefinition Width* /ColumnDefinition WidthAuto //Grid.ColumnDefinitions!--Label区域--ContentControl x:NameLabel Margin1 Template{TemplateBinding local:ControlAttachProperty.LabelTemplate}Content{TemplateBinding local:ControlAttachProperty.Label}/!--内容区域--ScrollViewer x:NamePART_ContentHost BorderThickness0 Grid.Column1 IsTabStopFalse Margin2VerticalAlignmentStretch Background{x:Null} /!--附加内容区域--Border x:NamePART_AttachContent Grid.Column2 Margin2 VerticalAlignmentCenter HorizontalAlignmentCenter ContentControl VerticalAlignmentCenter VerticalContentAlignmentCenter Template{TemplateBinding local:ControlAttachProperty.AttachContent} //Border/Grid/GridControlTemplate.TriggersTrigger PropertyIsMouseOver ValueTrueSetter PropertyBorderBrush Value{Binding Path(local:ControlAttachProperty.MouseOverBorderBrush),RelativeSource{RelativeSource Self}}//TriggerTrigger PropertyIsFocused ValueTrueSetter PropertyBorderBrush Value{Binding Path(local:ControlAttachProperty.FocusBorderBrush),RelativeSource{RelativeSource Self}}//Trigger!--不可用--Trigger PropertyIsEnabled ValueFalseSetter TargetNamePART_Root PropertyOpacity Value{StaticResource DisableOpacity}/Setter/Trigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/Setter/Style!--TextBox包含清除Text按钮的样式--Style TargetType{x:Type PasswordBox} x:KeyClearButtonPasswordBox BasedOn{StaticResource DefaultPasswordBox}Setter Propertylocal:ControlAttachProperty.AttachContentSetter.ValueControlTemplatelocal:FButton FIcon#xe60a; Style{StaticResource FButton_Transparency} IsTabStopFalse FIconMargin0local:ControlAttachProperty.IsClearTextButtonBehaviorEnabledTrue Commandlocal:ControlAttachProperty.ClearTextCommand CommandParameter{Binding RelativeSource{RelativeSource FindAncestor,AncestorType{x:Type PasswordBox}}}Margin1,3,1,4 FIconSize14 Foreground{StaticResource TextForeground} CursorArrow//ControlTemplate/Setter.Value/Setter/Style!--TextBox包含附加属性Label的样式--Style TargetType{x:Type PasswordBox} x:KeyLabelPasswordBox BasedOn{StaticResource DefaultPasswordBox}Setter Propertylocal:ControlAttachProperty.LabelTemplate Setter.ValueControlTemplate TargetTypeContentControlBorder Width60 Background{StaticResource TextLabelBackground}TextBlock VerticalAlignmentCenter HorizontalAlignmentRight Margin3 Text{TemplateBinding Content}/TextBlock/Border/ControlTemplate/Setter.Value/Setter/Style!--TextBox包含附加属性Label以及ClearText按钮的样式--Style TargetType{x:Type PasswordBox} x:KeyLabelClearButtonPasswordBox BasedOn{StaticResource DefaultPasswordBox}Setter Propertylocal:ControlAttachProperty.LabelTemplate Setter.ValueControlTemplate TargetTypeContentControlBorder Width60 Background{StaticResource TextLabelBackground}TextBlock VerticalAlignmentCenter HorizontalAlignmentRight Margin3 Text{TemplateBinding Content}/TextBlock/Border/ControlTemplate/Setter.Value/SetterSetter Propertylocal:ControlAttachProperty.AttachContentSetter.ValueControlTemplatelocal:FButton FIcon#xe60a; Style{StaticResource FButton_Transparency} IsTabStopFalse FIconMargin0local:ControlAttachProperty.IsClearTextButtonBehaviorEnabledTrue Commandlocal:ControlAttachProperty.ClearTextCommand CommandParameter{Binding RelativeSource{RelativeSource FindAncestor,AncestorType{x:Type PasswordBox}}}Margin0,3,1,4 FIconSize14 Foreground{StaticResource TextForeground} CursorArrow//ControlTemplate/Setter.Value/Setter/Style 使用示例及效果     原文地址http://www.cnblogs.com/anding/p/4970845.html转载于:https://www.cnblogs.com/mqxs/p/10142316.html
http://www.zqtcl.cn/news/828986/

相关文章:

  • 山西住房建设厅官方网站建设部建造师网站
  • 加大门户网站安全制度建设wordpress切换数据库
  • 百度代理服务器株洲seo优化
  • 即刻搜索网站提交入口网站中的打赏怎么做的
  • 电子商务网站建设课后作业开发公司管理制度
  • mysql同一数据库放多少个网站表优化大师windows
  • 微信小程序插件开发seo的网站建设
  • 婚纱摄影网站建设方案WordPress 同步网易博客
  • 上海长宁网站建设公司python语言基础
  • 官方网站怎样做餐饮业手机php网站
  • 网站建设企业有哪些内容十九届六中全会
  • 如何管理手机网站首页怎么建设一个社交网站
  • 网站规则山东网站备案网站
  • 成都网站制作龙兵科技做网站原型图用什么软件
  • 鄂州网站网站建设做网站 用哪种
  • 医药公司网站建设厦门网站建设合同
  • 网站开发全程设计注册公司哪个网站
  • 广州大型网站设计公司网站总体设计怎么写
  • 福州网站制作工具搜索引擎营销的特点是什么
  • 安徽省建设干部网站新品网络推广
  • 做网站要实名吗怎样给一个公司做网站
  • 品牌官方网站建设大航母网站建设
  • 自己做音乐网站挣钱吗网站定制公司kinglink
  • 网站建设案例新闻随州程力网站建设
  • 国外网站平台龙岩天宫山缆车收费
  • 站长工具seo综合查询是什么湖北做网站
  • 青海网站建设价格建一个免费网站的流程
  • 网站备案中 解析地址asp.net企业网站框架
  • flash里鼠标可以跟随到网站上就不能跟随了蚌埠网站建设
  • 东莞茶山网站建设网络推广方案ppt