搭建网站需要什么语言,网站的系统建设方式有哪些,电商网站设计的准则是什么,58同城 网站开发原文:WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展一#xff0e;前言 申明#xff1a;WPF自定义控件与样式是一个系列文章#xff0c;前后是有些关联的#xff0c;但大多是按照由简到繁的顺序逐步发布的等#xff0c;若有不明白的地方可以参考本…原文:WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展一前言 申明WPF自定义控件与样式是一个系列文章前后是有些关联的但大多是按照由简到繁的顺序逐步发布的等若有不明白的地方可以参考本系列前面的文章文末附有部分文章链接。 本文主要内容 日历控件Calendar自定义样式 日期控件DatePicker自定义样式及Label标签、水印、清除日期功能扩展 二Calendar自定义样式 先看看效果 从上面图可以看出日历的显示其实有三种状态如下面的分解图 日部分的显示 月部分的显示 年部分的显示 因此一个完整的日历就包含多个部分首先是日按钮的样式 !--Day按钮样式--Style x:KeyCalendarDayButtonStyle TargetType{x:Type CalendarDayButton}Setter PropertyMinWidth Value28 /Setter PropertyMinHeight Value5 /Setter PropertyFontFamily Value{StaticResource FontFamily} /Setter PropertyHorizontalContentAlignment ValueCenter /Setter PropertyVerticalContentAlignment ValueCenter /Setter PropertyBackground ValueTransparent /Setter PropertyForeground Value{StaticResource TextForeground} /Setter PropertyMargin Value0 /Setter PropertyIsTabStop ValueFalse /Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type CalendarDayButton}Grid x:NameGrid Margin{TemplateBinding Margin}Border x:NameBg Background{TemplateBinding Background} /ContentPresenter x:NameNormalText HorizontalAlignment{TemplateBinding HorizontalContentAlignment}Margin5,2,5,2 VerticalAlignment{TemplateBinding VerticalContentAlignment}TextElement.Foreground{TemplateBinding Foreground} //GridControlTemplate.TriggersTrigger PropertyIsSelected ValueTrueSetter PropertyBackground Value{StaticResource ItemSelectedBackground}/SetterSetter PropertyForeground Value{StaticResource ItemSelectedForeground}/Setter/TriggerTrigger PropertyIsToday ValueTrueSetter PropertyBackground Value{StaticResource ItemHighlighteBackground}/SetterSetter PropertyForeground Value{StaticResource ItemHighlighteForeground}/Setter/TriggerTrigger PropertyIsMouseOver ValueTrueSetter PropertyBackground Value{StaticResource ItemMouseOverBackground}/SetterSetter PropertyForeground Value{StaticResource ItemMouseOverForeground}/Setter/Trigger!--不可用日期--Trigger PropertyIsBlackedOut ValueTrueSetter PropertyOpacity Value{StaticResource DisableOpacity} TargetNameGrid/Setter/Trigger!--不在当月的日期--Trigger PropertyIsInactive ValueTrueSetter PropertyOpacity Value0.65 TargetNameGrid/Setter/TriggerTrigger PropertyIsEnabled ValueFalseSetter PropertyOpacity Value{StaticResource DisableOpacity} TargetNameGrid/Setter/Trigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/Setter/Style View Code 日历日期面板样式 !--日历日期面板样式--Style x:KeyCalendarItemStyle TargetType{x:Type CalendarItem}Setter PropertyMargin Value0,1,0,1 /Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type CalendarItem}ControlTemplate.Resources!-- 头部星期样式--DataTemplate x:Key{x:Static CalendarItem.DayTitleTemplateResourceKey}TextBlock Text{Binding} FontWeightBold FontFamily{StaticResource FontFamily} Foreground{StaticResource PressedForeground}FontSize{StaticResource HeaderFontSize} HorizontalAlignmentCenter VerticalAlignmentCenter Margin0,6,0,6 Opacity0.8 //DataTemplate/ControlTemplate.ResourcesGrid x:NamePART_RootBorder BorderBrush{TemplateBinding BorderBrush} BorderThickness1 Background{TemplateBinding Background} Margin{TemplateBinding Margin}Grid Margin2Grid.ColumnDefinitionsColumnDefinition WidthAuto //Grid.ColumnDefinitionsGrid.RowDefinitionsRowDefinition HeightAuto /RowDefinition Height* //Grid.RowDefinitions!--Header--Grid Grid.Row0 HorizontalAlignmentStretch Background{StaticResource HeaderBackground}Grid.ColumnDefinitionsColumnDefinition Width*/ColumnDefinition Width2*/ColumnDefinition Width*//Grid.ColumnDefinitionslocal:FButton x:NamePART_HeaderButton FontWeightBold Style{StaticResource FButton_Transparency}FocusableFalse Grid.Column1 FIcon{x:Null}/local:FButton x:NamePART_PreviousButton Style{StaticResource FButton_Transparency}FocusableFalse Grid.Column0 FIconSize18 Content FIcon#xe65e;/local:FButton x:NamePART_NextButton Style{StaticResource FButton_Transparency}FocusableFalse Grid.Column2 FIconSize18 Content FIcon#xe605;//Grid!--PART_MonthView--Grid x:NamePART_MonthView HorizontalAlignmentStretch VerticalAlignmentStretch Margin6,1,6,6 Grid.Row1 VisibilityVisibleGrid.ColumnDefinitionsColumnDefinition Width* /ColumnDefinition Width* /ColumnDefinition Width* /ColumnDefinition Width* /ColumnDefinition Width* /ColumnDefinition Width* /ColumnDefinition Width* //Grid.ColumnDefinitionsGrid.RowDefinitionsRowDefinition Height* /RowDefinition Height* /RowDefinition Height* /RowDefinition Height* /RowDefinition Height* /RowDefinition Height* /RowDefinition Height* //Grid.RowDefinitions/Grid!--PART_YearView--Grid x:NamePART_YearView HorizontalAlignmentStretch VerticalAlignmentStretch Margin6,10,6,10 Grid.Row1 VisibilityHiddenGrid.ColumnDefinitionsColumnDefinition Width* /ColumnDefinition Width* /ColumnDefinition Width* /ColumnDefinition Width* //Grid.ColumnDefinitionsGrid.RowDefinitionsRowDefinition Height* /RowDefinition Height* /RowDefinition Height* //Grid.RowDefinitions/Grid/Grid/Border/GridControlTemplate.TriggersTrigger PropertyIsEnabled ValueFalseSetter PropertyOpacity TargetNamePART_Root Value{StaticResource DisableOpacity} //TriggerDataTrigger Binding{Binding DisplayMode, RelativeSource{RelativeSource FindAncestor, AncestorType{x:Type Calendar}}} ValueYearSetter PropertyVisibility TargetNamePART_MonthView ValueHidden /Setter PropertyVisibility TargetNamePART_YearView ValueVisible //DataTrigger!--Decade 美 [dɛked] n. 十年十年期十--DataTrigger Binding{Binding DisplayMode, RelativeSource{RelativeSource FindAncestor, AncestorType{x:Type Calendar}}} ValueDecadeSetter PropertyVisibility TargetNamePART_MonthView ValueHidden /Setter PropertyVisibility TargetNamePART_YearView ValueVisible //DataTrigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/Setter/Style View Code 年月按钮样式 !--年、月按钮样式--Style x:KeyCalendarButtonStyle TargetType{x:Type CalendarButton}Setter PropertyBackground ValueTransparent /Setter PropertyMinWidth Value40 /Setter PropertyMinHeight Value42 /Setter PropertyFontSize Value{StaticResource FontSize} /Setter PropertyFontFamily Value{StaticResource FontFamily} /Setter PropertyHorizontalContentAlignment ValueCenter /Setter PropertyVerticalContentAlignment ValueCenter /Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type CalendarButton}Grid x:NameGrid Margin{TemplateBinding Margin}Border x:NameBg Background{TemplateBinding Background} /ContentPresenter x:NameNormalText HorizontalAlignment{TemplateBinding HorizontalContentAlignment}Margin5,2,5,2 VerticalAlignment{TemplateBinding VerticalContentAlignment}TextElement.Foreground{TemplateBinding Foreground} //GridControlTemplate.TriggersTrigger PropertyIsFocused ValueTrueSetter PropertyBackground Value{StaticResource ItemSelectedBackground}/SetterSetter PropertyForeground Value{StaticResource ItemSelectedForeground}/Setter/TriggerTrigger PropertyIsMouseOver ValueTrueSetter PropertyBackground Value{StaticResource ItemMouseOverBackground}/SetterSetter PropertyForeground Value{StaticResource ItemMouseOverForeground}/Setter/Trigger!--不在当月的日期--Trigger PropertyIsInactive ValueTrueSetter PropertyOpacity Value0.8 TargetNameGrid/Setter/TriggerTrigger PropertyIsEnabled ValueFalseSetter PropertyOpacity Value{StaticResource DisableOpacity} TargetNameGrid/Setter/Trigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/Setter/Style View Code 最后综合以上的样式定义我们需要的Calendar样式就差一步之遥了。 !--默认日历样式--Style x:KeyDefaultCalendar TargetType{x:Type Calendar}Setter PropertySnapsToDevicePixels ValueTrue /Setter PropertyForeground Value{StaticResource TextForeground} /Setter PropertyCalendarDayButtonStyle Value{StaticResource CalendarDayButtonStyle} /Setter PropertyCalendarItemStyle Value{StaticResource CalendarItemStyle} /Setter PropertyCalendarButtonStyle Value{StaticResource CalendarButtonStyle} /Setter PropertyBackground Value{StaticResource PopupBackground} /Setter PropertyBorderBrush Value{StaticResource ControlBorderBrush} /Setter PropertyBorderThickness Value1 /Setter PropertyFontSize Value13 /Setter PropertyFontFamily Value{StaticResource FontFamily} /Setter PropertyIsTodayHighlighted ValueTrue /Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type Calendar}StackPanel x:NamePART_Root HorizontalAlignmentCenter BackgroundTransparentCalendarItem x:NamePART_CalendarItem BorderBrush{TemplateBinding BorderBrush} FontSize{TemplateBinding FontSize}FontFamily{TemplateBinding FontFamily}BorderThickness{TemplateBinding BorderThickness} Background{TemplateBinding Background}Style{TemplateBinding CalendarItemStyle} //StackPanel/ControlTemplate/Setter.Value/Setter/Style 三DatePicker自定义样式及Label标签、水印、清除日期功能扩展 有了上面的日历样式下面做DatePicker的样式就好办了其实就是TextBoxButtonCalendar。此处的实现同上一篇WPF自定义控件与样式(3)-TextBox RichTextBox PasswordBox样式思路基本一致因此本文就不做更多的解释可以参考本系列前面的文章末尾附录有链接先看看效果图 3.1 DatePicker自定义样式 基本样式定义 Style x:KeyDefaultDatePicker TargetType{x:Type DatePicker}Setter PropertyForeground Value{StaticResource TextForeground} /Setter PropertyBackground Value{StaticResource TextBackground} /Setter PropertyBorderThickness Value1 /Setter PropertyBorderBrush Value{StaticResource ControlBorderBrush} /Setter Propertylocal:ControlAttachProperty.FocusBorderBrush Value{StaticResource FocusBorderBrush} /Setter Propertylocal:ControlAttachProperty.MouseOverBorderBrush Value{StaticResource MouseOverBorderBrush} /Setter Propertylocal:ControlAttachProperty.FIconSize Value21 /Setter PropertyFontFamily Value{StaticResource FontFamily} /Setter PropertyFontSize Value{StaticResource FontSize} /Setter PropertyMinHeight Value26 /Setter PropertyHeight Value30 /Setter PropertyWidth Value200 /Setter PropertyHorizontalContentAlignment ValueStretch /Setter PropertyIsTodayHighlighted ValueTrue /Setter PropertyPadding Value0 /Setter PropertyMargin Value0 /Setter PropertySelectedDate Value{x:Null} /Setter PropertySnapsToDevicePixels ValueTrue /Setter PropertyCalendarStyle Value{StaticResource DefaultCalendar} /Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type DatePicker}Grid x:NamePART_RootBorder x:NameBg SnapsToDevicePixels{TemplateBinding SnapsToDevicePixels}BorderBrush{TemplateBinding BorderBrush} BorderThickness{TemplateBinding BorderThickness}Background{TemplateBinding Background} /Grid x:NamePART_InnerGrid Margin0Grid.ColumnDefinitionsColumnDefinition WidthAuto /ColumnDefinition Width* /ColumnDefinition WidthAuto /ColumnDefinition WidthAuto //Grid.ColumnDefinitions!--Label区域--ContentControl x:NameLabel Template{TemplateBinding local:ControlAttachProperty.LabelTemplate} IsTabStopFalse IsHitTestVisibleFalseContent{TemplateBinding local:ControlAttachProperty.Label} Margin1,1,0,1/!--附加内容区域--Border x:NamePART_AttachContent Grid.Column2 VerticalAlignmentCenter HorizontalAlignmentCenter ContentControl VerticalAlignmentCenter VerticalContentAlignmentCenter Template{TemplateBinding local:ControlAttachProperty.AttachContent}//Border!--下拉按钮--ToggleButton x:NamePART_DropDownToggle IsTabStopFalse Style{StaticResource FIconToggleButton} IsChecked{Binding IsDropDownOpen, ModeTwoWay, RelativeSource{RelativeSource TemplatedParent}} Margin1,1,3,1Grid.Column3 local:ControlAttachProperty.FIconSize{TemplateBinding local:ControlAttachProperty.FIconSize}Background{TemplateBinding local:ControlAttachProperty.FocusBackground}/!--水印--Border Grid.Column1TextBlock x:NameMessage Padding0 VisibilityCollapsed Text{TemplateBinding local:ControlAttachProperty.Watermark} Foreground{TemplateBinding Foreground} IsHitTestVisibleFalse Opacity{StaticResource WatermarkOpacity} HorizontalAlignmentLeft TextAlignmentCenter VerticalAlignmentCenter Margin5,2,5,2 //Border!--Date内容区--Grid Grid.Column1TextBox x:NamePART_TextBox Style{StaticResource EditableTextBoxStyle} HorizontalAlignmentStretch SnapsToDevicePixels{TemplateBinding SnapsToDevicePixels} IsHitTestVisibleTrue IsReadOnlyTrueFontFamily{TemplateBinding FontFamily} Foreground{TemplateBinding Foreground} FontSize{TemplateBinding FontSize} Text{Binding PathSelectedDate,ModeOneWay,RelativeSource{RelativeSource TemplatedParent},StringFormat{StaticResource DateFormat}}//Grid!--弹出日历--Popup x:NamePART_Popup AllowsTransparencyTrue PlacementBottomPlacementTarget{Binding ElementNamePART_Root} StaysOpenFalse //Grid/GridControlTemplate.Triggers!--1.显示水印--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}}//TriggerTrigger PropertyIsKeyboardFocusWithin ValueTrueSetter PropertyBorderBrush Value{Binding Path(local:ControlAttachProperty.FocusBorderBrush),RelativeSource{RelativeSource Self}}//TriggerTrigger PropertyIsEnabled ValueFalseSetter TargetNamePART_Root PropertyOpacity Value{StaticResource DisableOpacity} //Trigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/Setter/Style View Code 水印效果 使用示例 DatePicker Margin3 core:ControlAttachProperty.Watermark妈的快输入日期/DatePicker Margin3 SelectedDate{x:Static system:DateTime.Today}/DatePicker Margin3 IsEnabledFalse SelectedDate{x:Static system:DateTime.Today}/ 3.2 Label标签 通过扩展基础样式中的标签附加属性实现定义样式代码 !--DatePicker包含附加属性Label的样式 LabelDatePicker --Style TargetType{x:Type DatePicker} x:KeyLabelDatePicker BasedOn{StaticResource DefaultDatePicker}Setter PropertyWidth Value260/SetterSetter Propertylocal:ControlAttachProperty.LabelTemplate Setter.ValueControlTemplate TargetTypeContentControlBorder Width60 Background{StaticResource TextLabelBackground}TextBlock VerticalAlignmentCenter HorizontalAlignmentRight Margin2 Text{TemplateBinding Content}/TextBlock/Border/ControlTemplate/Setter.Value/Setter/Style 效果图 使用示例 DatePicker Margin3 Style{StaticResource LabelClearButtonDatePicker} core:ControlAttachProperty.Watermark选择出生日期 core:ControlAttachProperty.Label出生日期/
DatePicker Margin3 Style{StaticResource LabelDatePicker} core:ControlAttachProperty.Label死亡日期 SelectedDate{x:Static system:DateTime.Today}/
DatePicker Margin3 Style{StaticResource LabelDatePicker} core:ControlAttachProperty.Label非法日期 IsEnabledFalse SelectedDate{x:Static system:DateTime.Today}/ 3.3 清除日期值的功能扩展 此功能在前面的文章有介绍过参考本文末尾链接效果在上面的图片中可以看到。样式代码 !--DatePicker包含清除Text按钮的样式 ClearButtonDatePicker --Style TargetType{x:Type DatePicker} x:KeyClearButtonDatePicker BasedOn{StaticResource DefaultDatePicker}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 DatePicker}}}Margin1,3,0,4 FIconSize14 Foreground{StaticResource TextForeground} CursorArrow//ControlTemplate/Setter.Value/Setter/Style!--DatePicker包含附加属性Label以及ClearText按钮的样式 LabelClearButtonDatePicker --Style TargetType{x:Type DatePicker} x:KeyLabelClearButtonDatePicker BasedOn{StaticResource DefaultDatePicker}Setter PropertyWidth Value280/SetterSetter Propertylocal:ControlAttachProperty.LabelTemplate Setter.ValueControlTemplate TargetTypeContentControlBorder Width60 Background{StaticResource TextLabelBackground}TextBlock VerticalAlignmentCenter HorizontalAlignmentRight Margin2 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 DatePicker}}}Margin0,3,0,4 FIconSize14 Foreground{StaticResource TextForeground} CursorArrow//ControlTemplate/Setter.Value/Setter/Style 使用示例 DatePicker Margin3 Style{StaticResource ClearButtonDatePicker}/
DatePicker Margin3 Style{StaticResource LabelClearButtonDatePicker} core:ControlAttachProperty.Watermark选择出生日期 core:ControlAttachProperty.Label出生日期/ 附录参考引用 WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与样式(3)-TextBox RichTextBox PasswordBox样式、水印、Label标签、功能扩展 WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式 版权所有文章来源http://www.cnblogs.com/anding 个人能力有限本文内容仅供学习、探讨欢迎指正、交流。