效果图如下
控件的背景 和按钮的背景样式 都是用图片做的,本次就不上传了 只传代码
前台样式代码
<Style x:Key="MeetingTextBox" TargetType="{x:Type TextBox}"> <Setter Property="Height" Value="28"></Setter> <Setter Property="FontSize" Value="14"></Setter> <Setter Property="BorderThickness" Value="0"></Setter> <Setter Property="common:PamirAttachProperty.CornerRadius" Value="3"></Setter> <Setter Property="CaretBrush" Value="#C0E9FC"></Setter> <Setter Property="Foreground" Value="#C0E9FC"></Setter> <Setter Property="VerticalAlignment" Value="Center"></Setter> <Setter Property="Background"> <Setter.Value> <SolidColorBrush Color="#3C5270" Opacity="0.6"></SolidColorBrush> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Grid x:Name="PART_Root"> <Border x:Name="Bg" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" CornerRadius="{TemplateBinding common:PamirAttachProperty.CornerRadius}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> <Grid x:Name="PART_InnerGrid"> <!--内容区域--> <ScrollViewer x:Name="PART_ContentHost" BorderThickness="0" IsTabStop="False" Margin="1,0,0,0" VerticalAlignment="{TemplateBinding VerticalAlignment}" Background="{x:Null}"/> <!--水印--> <TextBlock x:Name="Message" Padding="{TemplateBinding Padding}" Visibility="Collapsed" Text="{TemplateBinding common:PamirAttachProperty.Watermark}" Foreground="{TemplateBinding Foreground}" IsHitTestVisible="False" Opacity="0.5" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}" Margin="5,2" /> </Grid> </Border> </Grid> <ControlTemplate.Triggers> <!--显示水印--> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}" Value=""> <Setter TargetName="Message" Property="Visibility" Value="Visible" /> </DataTrigger> <!--<Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" Value="#C0E9FC"/> <Setter Property="BorderThickness" Value="1"></Setter> </Trigger>--> <Trigger Property="IsFocused" Value="True"> <Setter Property="BorderBrush" Value="#C0E9FC"/> <Setter Property="BorderThickness" Value="0.5"></Setter> </Trigger> <!--不可用--> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="PART_Root" Property="Opacity" Value="0.5" /> </Trigger> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" /> <Setter Property="BorderBrush" Value="Red" /> </Trigger> <!--只读时,禁用PART_AttachContent--> <!--<Trigger Property="IsReadOnly" Value="True"> <Setter TargetName="PART_AttachContent" Property="IsEnabled" Value="False" /> <Setter TargetName="Bg" Property="Opacity" Value="0.88" /> <Setter TargetName="PART_ContentHost" Property="Opacity" Value="0.88" /> <Setter TargetName="Label" Property="Opacity" Value="0.88" /> </Trigger>--> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="ComboBoxItem" x:Key="MeetingComboBoxItemStyle"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="RenderOptions.ClearTypeHint" Value="Enabled" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="Height" Value="28" /> <Setter Property="Foreground" Value="#C0E9FC"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ComboBoxItem"> <Grid Background="{TemplateBinding Background}" Margin="0,0.5"> <Border x:Name="ItemBackground" IsHitTestVisible="False" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" /> <ContentPresenter x:Name="contentPresenter" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="ItemBackground" Property="Background" Value="#0A78D5" /> <Setter Property="Foreground" Value="White"></Setter> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="ItemBackground" Property="Background" Value="#0A78D5" /> <Setter Property="Foreground" Value="White"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="ComboxButton" TargetType="{x:Type ToggleButton}"> <Setter Property="BorderThickness" Value="0"></Setter> <Setter Property="BorderBrush" Value="Transparent"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Border x:Name="PART_root"> <Grid Background="{TemplateBinding Background}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="Auto"></ColumnDefinition> </Grid.ColumnDefinitions> <Border x:Name="PART_btn" Grid.Column="1" Width="20" Height="20"></Border> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="PART_btn" Property="Background"> <Setter.Value> <ImageBrush ImageSource="pack://application:,,,/PamirNms;component/Resources/imgs/sys/branch-closed.png"></ImageBrush> </Setter.Value> </Setter> </Trigger> <Trigger Property="IsChecked" Value="False"> <Setter TargetName="PART_btn" Property="Background"> <Setter.Value> <ImageBrush ImageSource="pack://application:,,,/PamirNms;component/Resources/imgs/sys/combobox_down_disabled.png"></ImageBrush> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="DateTimePickerButton" TargetType="{x:Type ToggleButton}"> <Setter Property="BorderThickness" Value="0"></Setter> <Setter Property="BorderBrush" Value="Transparent"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Border x:Name="PART_root"> <Grid Background="{TemplateBinding Background}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="Auto"></ColumnDefinition> </Grid.ColumnDefinitions> <Border x:Name="PART_btn" Grid.Column="1" Width="20" Height="20"></Border> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="PART_btn" Property="Background"> <Setter.Value> <ImageBrush ImageSource="pack://application:,,,/PamirNms;component/Resources/imgs/sys/calendar.png"></ImageBrush> </Setter.Value> </Setter> </Trigger> <Trigger Property="IsChecked" Value="False"> <Setter TargetName="PART_btn" Property="Background"> <Setter.Value> <ImageBrush ImageSource="pack://application:,,,/PamirNms;component/Resources/imgs/sys/calendar.png"></ImageBrush> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="MeetingCombobox" TargetType="{x:Type ComboBox}"> <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/> <Setter Property="Height" Value="30" /> <Setter Property="Foreground" Value="#C0E9FC" /> <Setter Property="Background"> <Setter.Value> <SolidColorBrush Color="#3C5270" Opacity="0.6"></SolidColorBrush> </Setter.Value> </Setter> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="#C0E9FC" /> <Setter Property="common:PamirAttachProperty.CornerRadius" Value="3"></Setter> <Setter Property="FontSize" Value="14" /> <Setter Property="ItemContainerStyle" Value="{StaticResource MeetingComboBoxItemStyle}" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="MaxDropDownHeight" Value="400" /> <Setter Property="ScrollViewer.CanContentScroll" Value="False" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ComboBox}"> <Grid x:Name="PART_Root"> <Border x:Name="Bg" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" CornerRadius="{TemplateBinding common:PamirAttachProperty.CornerRadius}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" /> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <!--下拉按钮--> <ToggleButton x:Name="PART_DropDownToggle" IsTabStop="False" Style="{StaticResource ComboxButton}" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="2 1 2 1" Grid.Column="0" Grid.ColumnSpan="2" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}"/> <!--水印--> <Border Grid.Column="0"> <TextBlock x:Name="Message" Padding="0" Visibility="Collapsed" Text="{TemplateBinding common:PamirAttachProperty.Watermark}" Foreground="{TemplateBinding Foreground}" IsHitTestVisible="False" Opacity="0.5" HorizontalAlignment="Left" TextAlignment="Center" VerticalAlignment="Center" Margin="5,2,5,2" /> </Border> <!--主内容区域--> <Grid Grid.Column="0" x:Name="ContentSite" Margin="2 0 0 0" > <ContentPresenter x:Name="PART_SelectedItem" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="Stretch" Margin="2,0,2,0" IsHitTestVisible="False" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> <!--文本编辑--> <TextBox x:Name="PART_EditableTextBox" Style="{StaticResource MeetingTextBox}" Visibility="Collapsed" HorizontalAlignment="Stretch" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" IsHitTestVisible="True" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" IsReadOnly="{TemplateBinding IsReadOnly}" FontFamily="{TemplateBinding FontFamily}" Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Text}" FontSize="{TemplateBinding FontSize}" /> </Grid> </Grid> <!--弹出下拉控件--> <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"> <Grid Width="{Binding ActualWidth, RelativeSource={RelativeSource TemplatedParent}}" MaxHeight="{Binding MaxDropDownHeight, RelativeSource={RelativeSource TemplatedParent}}"> <Border x:Name="PopupBorder" BorderThickness="{TemplateBinding BorderThickness}" HorizontalAlignment="Stretch" Height="Auto" BorderBrush="{TemplateBinding BorderBrush}" Background="#233757"> </Border> <ScrollViewer x:Name="DropDownScrollViewer" BorderThickness="0" Margin="1"> <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" /> </ScrollViewer> </Grid> </Popup> </Grid> <!--触发器--> <ControlTemplate.Triggers> <!--1.显示水印--> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}" Value=""> <Setter TargetName="Message" Property="Visibility" Value="Visible" /> </DataTrigger> <!--编辑模式--> <Trigger Property="IsEditable" Value="True"> <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible" /> <Setter TargetName="PART_SelectedItem" Property="Visibility" Value="Collapsed" /> <Setter TargetName="PART_DropDownToggle" Property="Grid.Column" Value="3" /> <Setter TargetName="PART_DropDownToggle" Property="Grid.ColumnSpan" Value="1" /> <Setter TargetName="PART_DropDownToggle" Property="Background" Value="Transparent" /> <Setter Property="IsTabStop" Value="false" /> <Setter TargetName="PART_DropDownToggle" Property="Focusable" Value="False" /> </Trigger> <Trigger Property="IsFocused" Value="True"> <Setter Property="BorderThickness" Value="1"/> </Trigger> <Trigger Property="IsKeyboardFocusWithin" Value="True"> <Setter Property="BorderThickness" Value="1"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="PART_Root" Property="Opacity" Value="0.88"></Setter> </Trigger> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" /> <Setter Property="BorderBrush" Value="Red"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="{x:Type ToolTip}"> <Setter Property="MinHeight" Value="45"></Setter> <Setter Property="Foreground" Value="#C0E9FC"></Setter> <Setter Property="Background"> <Setter.Value> <ImageBrush Stretch="Fill" ImageSource="pack://application:,,,/PamirNms;component/Resources/imgs/sys/skin_win_bk.png"></ImageBrush> </Setter.Value> </Setter> </Style> <sys:Double x:Key="ScrollBarSize">12</sys:Double> <!--滚动条滑块两边按钮样式--> <Style x:Key="ScrollBarTrackButton" TargetType="{x:Type RepeatButton}"> <Setter Property="Background" Value="Transparent"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type RepeatButton}"> <Border Background="Transparent"></Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--滚动条滑块样式--> <ControlTemplate x:Key="ThumbTemplate" TargetType="Thumb"> <Grid> <Border x:Name="Bg" CornerRadius="2" MinHeight="{TemplateBinding MinHeight}" Margin="2" SnapsToDevicePixels="True" Background="#877F7F"> <!--<Border.Background> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Color="#C7C0C0" Offset="0.15"/> <GradientStop Color="#AFA9A9" Offset=".5"/> <GradientStop Color="#989494" Offset=".5"/> <GradientStop Color="#858585" Offset="1"/> </LinearGradientBrush> </Border.Background>--> </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Black" TargetName="Bg"></Setter> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value="0.5" TargetName="Bg"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> <!--水平滚滚动条模板--> <ControlTemplate x:Key="HorizontalScrollBar" TargetType="{x:Type ScrollBar}"> <Grid x:Name="HorizontalRoot" Height="{TemplateBinding Height}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <!--外部背景,好像不用更好看--> <!--<Border x:Name="Bg" Grid.Column="0" Grid.ColumnSpan="3" CornerRadius="0" Opacity="0" Background="#858585"/>--> <!--内部背景--> <Border x:Name="BgInner" Grid.Column="1" Margin="0" SnapsToDevicePixels="True" Opacity="0.3" CornerRadius="6" Background="#877F7F"/> <!--左按钮--> <!--<Border Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center"> <RepeatButton local:ControlAttachProperty.FIcon="" Style="{StaticResource ScrollBarButton}" x:Name="HorizontalSmallDecrease" IsTabStop="False" Interval="50" Margin="0,1,0,0" Command="ScrollBar.LineLeftCommand"/> </Border>--> <!--中间滑动区域--> <Track x:Name="PART_Track" IsDirectionReversed="False" Grid.Column="1"> <!--左滑块--> <Track.DecreaseRepeatButton> <RepeatButton x:Name="HorizontalLargeDecrease" Command="ScrollBar.PageLeftCommand" IsTabStop="False" Interval="50" Style="{DynamicResource ScrollBarTrackButton}" /> </Track.DecreaseRepeatButton> <!--中间滑块 Margin="1" VerticalAlignment="Center" VerticalContentAlignment="Center" --> <Track.Thumb> <Thumb Template="{StaticResource ThumbTemplate}" /> </Track.Thumb> <!--右滑块--> <Track.IncreaseRepeatButton> <RepeatButton x:Name="HorizontalLargeIncrease" Command="ScrollBar.PageRightCommand" IsTabStop="False" Interval="50" Style="{DynamicResource ScrollBarTrackButton}" /> </Track.IncreaseRepeatButton> </Track> <!--右按钮--> <!--<Border Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center"> <RepeatButton local:ControlAttachProperty.FIcon="" Style="{StaticResource ScrollBarButton}" IsTabStop="False" Interval="50" Margin="0,1,0,0" Command="ScrollBar.LineRightCommand"/> </Border>--> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="BgInner" Property="Opacity" Value="0.5"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> <!--垂直滚滚动条模板--> <ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}"> <Grid x:Name="VerticalRoot" Height="{TemplateBinding Height}"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <!--外部背景,好像不用更好看--> <!--<Border x:Name="Bg" Grid.Row="0" Grid.RowSpan="3" CornerRadius="0" Opacity="0" Background="#858585"/>--> <!--内部背景--> <Border x:Name="BgInner" Grid.Row="1" Margin="0" CornerRadius="6" SnapsToDevicePixels ="True" Opacity="0.3" Background="#877F7F"/> <!--上按钮--> <!--<Border Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="VerticalSmallDecrease"> <RepeatButton local:ControlAttachProperty.FIcon="" Style="{StaticResource ScrollBarButton}" IsTabStop="False" Interval="50" Margin="0" Command="ScrollBar.LineUpCommand"/> </Border>--> <!--中间滑动区域--> <Track x:Name="PART_Track" IsDirectionReversed="true" Grid.Row="1"> <!--上滑块--> <Track.DecreaseRepeatButton> <RepeatButton x:Name="HorizontalLargeDecrease" Command="ScrollBar.PageUpCommand" IsTabStop="False" Interval="50" Style="{DynamicResource ScrollBarTrackButton}" /> </Track.DecreaseRepeatButton> <!--中间滑块--> <Track.Thumb> <Thumb Template="{StaticResource ThumbTemplate}" MinHeight="15"/> </Track.Thumb> <!--下滑块--> <Track.IncreaseRepeatButton> <RepeatButton x:Name="HorizontalLargeIncrease" Command="ScrollBar.PageDownCommand" IsTabStop="False" Interval="50" Style="{DynamicResource ScrollBarTrackButton}" /> </Track.IncreaseRepeatButton> </Track> <!--下按钮--> <!--<Border Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="VerticalSmallIncrease"> <RepeatButton local:ControlAttachProperty.FIcon="" Style="{StaticResource ScrollBarButton}" IsTabStop="False" Interval="50" Margin="0" Command="ScrollBar.LineDownCommand"/> </Border>--> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="BgInner" Property="Opacity" Value="0.5"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> <!--ScrollBar样式--> <Style x:Key="DefaultScrollBar" TargetType="{x:Type ScrollBar}"> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="OverridesDefaultStyle" Value="true" /> <Style.Triggers> <Trigger Property="Orientation" Value="Horizontal"> <Setter Property="Template" Value="{StaticResource HorizontalScrollBar}" /> <Setter Property="Height" Value="{StaticResource ScrollBarSize}" /> </Trigger> <Trigger Property="Orientation" Value="Vertical"> <Setter Property="Template" Value="{StaticResource VerticalScrollBar}" /> <Setter Property="Width" Value="{StaticResource ScrollBarSize}" /> </Trigger> </Style.Triggers> </Style> <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource DefaultScrollBar}"></Style> <!--周六日日期颜色转换--> <control:DateConvertToColor x:Key="DateConvertToColor"></control:DateConvertToColor> <!--Day按钮样式--> <Style x:Key="CalendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}"> <Setter Property="MinWidth" Value="40" /> <Setter Property="MinHeight" Value="30" /> <Setter Property="FontFamily" Value="微软雅黑" /> <Setter Property="FontSize" Value="16" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Background" Value="Transparent" /> <Setter Property="Foreground" Value="#C0E9FC" /> <Setter Property="Margin" Value="0" /> <Setter Property="IsTabStop" Value="False" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type CalendarDayButton}"> <Grid x:Name="Grid" Margin="{TemplateBinding Margin}"> <Border x:Name="Bg" Background="{TemplateBinding Background}" /> <ContentPresenter x:Name="NormalText" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="5,2,5,2" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" > <TextElement.Foreground> <Binding RelativeSource="{RelativeSource TemplatedParent}" Converter="{StaticResource DateConvertToColor}"> </Binding> </TextElement.Foreground> </ContentPresenter> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="#0A78D5"></Setter> <Setter Property="Foreground" Value="White"></Setter> </Trigger> <Trigger Property="IsToday" Value="True"> <Setter Property="Background" Value="White"></Setter> <Setter Property="Foreground" Value="Black"></Setter> </Trigger> <!--<Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="{StaticResource ItemMouseOverBackground}"></Setter> <Setter Property="Foreground" Value="{StaticResource ItemMouseOverForeground}"></Setter> </Trigger>--> <!--不可用日期--> <Trigger Property="IsBlackedOut" Value="True"> <Setter Property="Opacity" Value="0.6" TargetName="Grid"></Setter> <Setter Property="Foreground" Value="#3C5270"></Setter> </Trigger> <!--不在当月的日期--> <Trigger Property="IsInactive" Value="True"> <Setter Property="Opacity" Value="0.6" TargetName="Grid"></Setter> <Setter Property="Foreground" Value="#3C5270"></Setter> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value="0.5" TargetName="Grid"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="CalanderBtn" TargetType="{x:Type Button}"> <Setter Property="FontSize" Value="24"></Setter> <Setter Property="FontFamily" Value="正楷"></Setter> <Setter Property="Background" Value="Transparent"></Setter> <Setter Property="Foreground" Value="White"></Setter> <Setter Property="FontWeight" Value="Bold"></Setter> <Setter Property="VerticalAlignment" Value="Center"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="PART_root" Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <Grid VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}"> <TextBlock FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Content}" VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" ></TextBlock> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="CalanderYearBtn" TargetType="{x:Type Button}"> <Setter Property="FontSize" Value="18"></Setter> <Setter Property="FontFamily" Value="微软雅黑"></Setter> <Setter Property="Background" Value="Transparent"></Setter> <Setter Property="Foreground" Value="#C0E9FC"></Setter> <Setter Property="FontWeight" Value="Bold"></Setter> <Setter Property="HorizontalAlignment" Value="Center"></Setter> <Setter Property="VerticalAlignment" Value="Center"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="PART_root" Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <Grid VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}"> <TextBlock FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Content}" VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" ></TextBlock> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Cursor" Value="Hand"></Setter> </Trigger> </Style.Triggers> </Style> <!--日期标题颜色转换--> <control:DateTitleConvertToColor x:Key="DateTitleConvertToColor"></control:DateTitleConvertToColor> <!--日历日期面板样式--> <Style x:Key="CalendarItemStyle" TargetType="{x:Type CalendarItem}"> <Setter Property="Margin" Value="0,1,0,1" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type CalendarItem}"> <ControlTemplate.Resources> <!-- 头部星期样式--> <DataTemplate x:Key="{x:Static CalendarItem.DayTitleTemplateResourceKey}"> <StackPanel> <StackPanel.Background> <SolidColorBrush Color="#3C5270" Opacity="0.6"></SolidColorBrush> </StackPanel.Background> <!--<TextBlock Text="{Binding}" FontFamily="微软雅黑" Foreground="#C0E9FC" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,6,0,6" Opacity="0.8" />--> <TextBlock Text="{Binding}" FontFamily="微软雅黑" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,6,0,6" Opacity="0.8" Foreground="{Binding Converter={StaticResource DateTitleConvertToColor}}"> </TextBlock> </StackPanel> </DataTemplate> </ControlTemplate.Resources> <Grid x:Name="PART_Root"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Background="{TemplateBinding Background}" Margin="{TemplateBinding Margin}"> <Grid Margin="2"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <!--Header--> <Grid Grid.Row="0" HorizontalAlignment="Stretch" Background="{TemplateBinding Background}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="2*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Button x:Name="PART_HeaderButton" FontWeight="Bold" Focusable="False" Grid.Column="1" Style="{StaticResource CalanderYearBtn}"/> <Button x:Name="PART_PreviousButton" Focusable="False" Grid.Column="0" Style="{StaticResource CalanderBtn}" Margin="5,0,0,0" Content="<" /> <Button x:Name="PART_NextButton" Focusable="False" Grid.Column="2" Style="{StaticResource CalanderBtn}" HorizontalAlignment="Right" Margin="0,0,5,0" Content=">" /> </Grid> <!--PART_MonthView--> <Grid x:Name="PART_MonthView" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="6,1,6,6" Grid.Row="1" Visibility="Visible"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> </Grid> <!--PART_YearView--> <Grid x:Name="PART_YearView" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="6,10,6,10" Grid.Row="1" Visibility="Hidden"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> </Grid> </Grid> </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" TargetName="PART_Root" Value="0.5" /> </Trigger> <DataTrigger Binding="{Binding DisplayMode, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Calendar}}}" Value="Year"> <Setter Property="Visibility" TargetName="PART_MonthView" Value="Hidden" /> <Setter Property="Visibility" TargetName="PART_YearView" Value="Visible" /> </DataTrigger> <!--Decade 美 ['dɛked] n. 十年,十年期;十--> <DataTrigger Binding="{Binding DisplayMode, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Calendar}}}" Value="Decade"> <Setter Property="Visibility" TargetName="PART_MonthView" Value="Hidden" /> <Setter Property="Visibility" TargetName="PART_YearView" Value="Visible" /> </DataTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--年、月按钮样式--> <Style x:Key="CalendarButtonStyle" TargetType="{x:Type CalendarButton}"> <Setter Property="Background" Value="Transparent" /> <Setter Property="MinWidth" Value="40" /> <Setter Property="MinHeight" Value="42" /> <Setter Property="FontSize" Value="14" /> <Setter Property="FontFamily" Value="微软雅黑" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type CalendarButton}"> <Grid x:Name="Grid" Margin="{TemplateBinding Margin}"> <Border x:Name="Bg" Background="{TemplateBinding Background}" /> <ContentPresenter x:Name="NormalText" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="5,2,5,2" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" TextElement.Foreground="{TemplateBinding Foreground}" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsFocused" Value="True"> <Setter Property="Background" Value="#071A34"></Setter> <Setter Property="Foreground" Value="#C0E9FC"></Setter> </Trigger> <!--<Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="{StaticResource ItemMouseOverBackground}"></Setter> <Setter Property="Foreground" Value="{StaticResource ItemMouseOverForeground}"></Setter> </Trigger>--> <!--不在当月的日期--> <Trigger Property="IsInactive" Value="True"> <Setter Property="Opacity" Value="0.6" TargetName="Grid"></Setter> <Setter Property="Foreground" Value="#3C5270"></Setter> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value="0.6" TargetName="Grid"></Setter> <Setter Property="Foreground" Value="#3C5270"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--默认日历样式--> <Style x:Key="DefaultCalendar" TargetType="{x:Type Calendar}"> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="Foreground" Value="#C0E9FC" /> <Setter Property="CalendarDayButtonStyle" Value="{StaticResource CalendarDayButtonStyle}" /> <Setter Property="CalendarItemStyle" Value="{StaticResource CalendarItemStyle}" /> <Setter Property="CalendarButtonStyle" Value="{StaticResource CalendarButtonStyle}" /> <Setter Property="Background" Value="#071A34" /> <Setter Property="BorderBrush" Value="#071A34" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="FontSize" Value="17" /> <Setter Property="FontFamily" Value="微软雅黑" /> <Setter Property="IsTodayHighlighted" Value="True" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Calendar}"> <CalendarItem x:Name="PART_CalendarItem" BorderBrush="{TemplateBinding BorderBrush}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" Foreground="{TemplateBinding Foreground}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Style="{TemplateBinding CalendarItemStyle}" /> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--自定义控件样式--> <Style TargetType="{x:Type control:DateTimePicker}"> <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/> <Setter Property="Height" Value="30" /> <Setter Property="Foreground" Value="#C0E9FC" /> <Setter Property="Background"> <Setter.Value> <SolidColorBrush Color="#3C5270" Opacity="0.6"></SolidColorBrush> </Setter.Value> </Setter> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="#C0E9FC" /> <Setter Property="common:PamirAttachProperty.CornerRadius" Value="3"></Setter> <Setter Property="FontSize" Value="14" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="ScrollViewer.CanContentScroll" Value="False" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ComboBox}"> <Grid x:Name="PART_Root"> <Border x:Name="Bg" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" /> <Grid x:Name="PART_InnerGrid" Margin="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <!--下拉按钮--> <ToggleButton x:Name="PART_DropDownToggle" IsTabStop="False" Style="{StaticResource DateTimePickerButton}" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="2 1 2 1" Grid.Column="0" Grid.ColumnSpan="2" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}"/> <!--水印--> <Border Grid.Column="0"> <TextBlock x:Name="Message" Padding="0" Visibility="Collapsed" Text="{TemplateBinding common:PamirAttachProperty.Watermark}" Foreground="{TemplateBinding Foreground}" IsHitTestVisible="False" Opacity="0.5" HorizontalAlignment="Left" TextAlignment="Center" VerticalAlignment="Center" Margin="5,2,5,2" /> </Border> <!--主内容区域--> <Grid Grid.Column="0" x:Name="ContentSite" Margin="2 0 0 0" > <ContentPresenter x:Name="PART_SelectedItem" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding Text}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="Stretch" Margin="2,0,20,0" IsHitTestVisible="False" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> </Grid> <!--弹出日历--> <Popup x:Name="PART_Popup" AllowsTransparency="True" Placement="Bottom" PlacementTarget="{Binding ElementName=PART_Root}" StaysOpen="True" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" > <Border Background="#08D4FE"> <Grid Margin="1,0,1,15"> <Grid.Background> <SolidColorBrush Color="#2D3E5F" Opacity="1"></SolidColorBrush> </Grid.Background> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <Grid Grid.Row="0" Background="#071A34"> <Calendar x:Name="PART_Calendar" HorizontalAlignment="Center" Grid.Row="0" Style="{StaticResource DefaultCalendar}" SelectedDate="{Binding SelectedDate,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay, RelativeSource={RelativeSource AncestorType=control:DateTimePicker}}" SelectionMode="SingleDate"></Calendar> </Grid> <Grid Grid.Row="1" SnapsToDevicePixels="True" Margin="0,5,0,0"> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Margin="3" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock Style="{StaticResource TextStyle}" Margin="10,0">时间:</TextBlock> <ComboBox Style="{StaticResource MeetingCombobox}" Width="100" MaxDropDownHeight="100" Height="25" Text="{Binding Hour,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=control:DateTimePicker}}"> <ComboBoxItem>00</ComboBoxItem> <ComboBoxItem>01</ComboBoxItem> <ComboBoxItem>02</ComboBoxItem> <ComboBoxItem>03</ComboBoxItem> <ComboBoxItem>04</ComboBoxItem> <ComboBoxItem>05</ComboBoxItem> <ComboBoxItem>06</ComboBoxItem> <ComboBoxItem>07</ComboBoxItem> <ComboBoxItem>08</ComboBoxItem> <ComboBoxItem>09</ComboBoxItem> <ComboBoxItem>10</ComboBoxItem> <ComboBoxItem>11</ComboBoxItem> <ComboBoxItem>12</ComboBoxItem> <ComboBoxItem>13</ComboBoxItem> <ComboBoxItem>14</ComboBoxItem> <ComboBoxItem>15</ComboBoxItem> <ComboBoxItem>16</ComboBoxItem> <ComboBoxItem>17</ComboBoxItem> <ComboBoxItem>18</ComboBoxItem> <ComboBoxItem>19</ComboBoxItem> <ComboBoxItem>20</ComboBoxItem> <ComboBoxItem>21</ComboBoxItem> <ComboBoxItem>22</ComboBoxItem> <ComboBoxItem>23</ComboBoxItem> </ComboBox> <TextBlock Style="{StaticResource TextStyle}" Margin="10,0">时</TextBlock> <ComboBox Style="{StaticResource MeetingCombobox}" MaxDropDownHeight="100" Width="100" Height="25" Text="{Binding Minute,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=control:DateTimePicker}}"> <ComboBoxItem>00</ComboBoxItem> <ComboBoxItem>01</ComboBoxItem> <ComboBoxItem>02</ComboBoxItem> <ComboBoxItem>03</ComboBoxItem> <ComboBoxItem>04</ComboBoxItem> <ComboBoxItem>05</ComboBoxItem> <ComboBoxItem>06</ComboBoxItem> <ComboBoxItem>07</ComboBoxItem> <ComboBoxItem>08</ComboBoxItem> <ComboBoxItem>09</ComboBoxItem> <ComboBoxItem>10</ComboBoxItem> <ComboBoxItem>11</ComboBoxItem> <ComboBoxItem>12</ComboBoxItem> <ComboBoxItem>13</ComboBoxItem> <ComboBoxItem>14</ComboBoxItem> <ComboBoxItem>15</ComboBoxItem> <ComboBoxItem>16</ComboBoxItem> <ComboBoxItem>17</ComboBoxItem> <ComboBoxItem>18</ComboBoxItem> <ComboBoxItem>19</ComboBoxItem> <ComboBoxItem>20</ComboBoxItem> <ComboBoxItem>21</ComboBoxItem> <ComboBoxItem>22</ComboBoxItem> <ComboBoxItem>23</ComboBoxItem> <ComboBoxItem>24</ComboBoxItem> <ComboBoxItem>25</ComboBoxItem> <ComboBoxItem>26</ComboBoxItem> <ComboBoxItem>27</ComboBoxItem> <ComboBoxItem>28</ComboBoxItem> <ComboBoxItem>29</ComboBoxItem> <ComboBoxItem>30</ComboBoxItem> <ComboBoxItem>31</ComboBoxItem> <ComboBoxItem>32</ComboBoxItem> <ComboBoxItem>33</ComboBoxItem> <ComboBoxItem>34</ComboBoxItem> <ComboBoxItem>35</ComboBoxItem> <ComboBoxItem>36</ComboBoxItem> <ComboBoxItem>37</ComboBoxItem> <ComboBoxItem>38</ComboBoxItem> <ComboBoxItem>39</ComboBoxItem> <ComboBoxItem>40</ComboBoxItem> <ComboBoxItem>41</ComboBoxItem> <ComboBoxItem>42</ComboBoxItem> <ComboBoxItem>43</ComboBoxItem> <ComboBoxItem>44</ComboBoxItem> <ComboBoxItem>45</ComboBoxItem> <ComboBoxItem>46</ComboBoxItem> <ComboBoxItem>47</ComboBoxItem> <ComboBoxItem>48</ComboBoxItem> <ComboBoxItem>49</ComboBoxItem> <ComboBoxItem>50</ComboBoxItem> <ComboBoxItem>51</ComboBoxItem> <ComboBoxItem>52</ComboBoxItem> <ComboBoxItem>53</ComboBoxItem> <ComboBoxItem>54</ComboBoxItem> <ComboBoxItem>55</ComboBoxItem> <ComboBoxItem>56</ComboBoxItem> <ComboBoxItem>57</ComboBoxItem> <ComboBoxItem>58</ComboBoxItem> <ComboBoxItem>59</ComboBoxItem> </ComboBox> <TextBlock Style="{StaticResource TextStyle}" Margin="10,0,10,0">分</TextBlock> </StackPanel> <StackPanel Grid.Row="1" Margin="3" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <Button ClickMode="Press" Style="{StaticResource PamirButton}" Margin="0,0,5,0" Command="{Binding OkCommand,RelativeSource={RelativeSource AncestorType=control:DateTimePicker}}">确定</Button> <Button ClickMode="Press" Style="{StaticResource PamirButton}" Margin="5,0,0,0" Command="{Binding CancelCommand,RelativeSource={RelativeSource AncestorType=control:DateTimePicker}}">取消</Button> </StackPanel> </Grid> </Grid> </Border> </Popup> </Grid> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsFocused" Value="True"> <Setter Property="BorderThickness" Value="1"/> </Trigger> <Trigger Property="IsKeyboardFocusWithin" Value="True"> <Setter Property="BorderThickness" Value="1"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="PART_Root" Property="Opacity" Value="0.88"></Setter> </Trigger> <Trigger Property="IsKeyboardFocusWithin" Value="True"> <Setter Property="BorderThickness" Value="1"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
后台代码
[TemplatePart(Name = "PART_Calendar",Type = typeof(System.Windows.Controls.Calendar))] public class DateTimePicker : ComboBox { public static readonly DependencyProperty DateAndTimeTextProperty = DependencyProperty.Register( "DateAndTimeText", typeof(string), typeof(DateTimePicker), new PropertyMetadata(default(string))); public string DateAndTimeText { get { return (string)GetValue(DateAndTimeTextProperty); } set { SetValue(DateAndTimeTextProperty, value); } } public static readonly DependencyProperty SelectedDateProperty = DependencyProperty.Register( "SelectedDate", typeof(DateTime?), typeof(DateTimePicker), new PropertyMetadata(default(DateTime?))); public DateTime? SelectedDate { get { return (DateTime?)GetValue(SelectedDateProperty); } set { SetValue(SelectedDateProperty, value); } } public static readonly DependencyProperty HourProperty = DependencyProperty.Register( "Hour", typeof(string), typeof(DateTimePicker), new PropertyMetadata(default(string))); public string Hour { get { return (string) GetValue(HourProperty); } set { SetValue(HourProperty, value); } } public static readonly DependencyProperty MinuteProperty = DependencyProperty.Register( "Minute", typeof(string), typeof(DateTimePicker), new PropertyMetadata(default(string))); public string Minute { get { return (string) GetValue(MinuteProperty); } set { SetValue(MinuteProperty, value); } } public ICommand OkCommand { get; set; } public ICommand CancelCommand { get; set; } protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { base.OnPropertyChanged(e); if (e.Property == DateAndTimeTextProperty) { this.Text = DateAndTimeText; string[] datetime = DateAndTimeText.Split(' '); if (datetime.Length == 2) { SelectedDate = DateTime.Parse(datetime[0]); string[] times = datetime[1].Split(':'); Hour = times[0]; Minute = times[1]; } } } public DateTimePicker() { OkCommand = new DelegateCommand(OkFunc); CancelCommand=new DelegateCommand(CancelFunc); this.Loaded += DateTimePicker_Loaded; } private void DateTimePicker_Loaded(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(DateAndTimeText)) { string date = $"{DateTime.Now:yyyy-MM-dd HH:mm}"; DateAndTimeText = date; } if (Template.FindName("PART_Calendar", this) is System.Windows.Controls.Calendar cander) { cander.PreviewMouseUp += (s, ce) => { if (Mouse.Captured is CalendarItem) { Mouse.Capture(null); } }; } } private void OkFunc() { if (SelectedDate != null) { string date = $"{SelectedDate?.ToString("yyyy-MM-dd")} {Hour}:{Minute}"; DateAndTimeText = date; } else { string date = $"{DateTime.Now:yyyy-MM-dd HH:mm}"; DateAndTimeText = date; } IsDropDownOpen = false; } private void CancelFunc() { IsDropDownOpen = false; } } /// <summary> /// 日期颜色转换器 /// </summary> public class DateConvertToColor : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var calendarDayButton = (CalendarDayButton)value; if (calendarDayButton.IsToday) { return Brushes.Black; } var dateTime = (DateTime)calendarDayButton.DataContext; if (!calendarDayButton.IsMouseOver && !calendarDayButton.IsSelected && !calendarDayButton.IsBlackedOut && (dateTime.DayOfWeek == DayOfWeek.Saturday || dateTime.DayOfWeek == DayOfWeek.Sunday)) return new SolidColorBrush(Color.FromArgb(255, 255, 47, 47)); else return new SolidColorBrush(Color.FromRgb(192, 233, 252)); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } /// <summary> /// 日期标题颜色转换器 /// </summary> public class DateTitleConvertToColor : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) { return new SolidColorBrush(Color.FromRgb(192, 233, 252)); } string day = value.ToString(); if (day == "六" || day == "日" || day.ToUpper() == "SAT" || day.ToUpper() == "SUN") { return new SolidColorBrush(Color.FromArgb(255, 255, 47, 47)); } return new SolidColorBrush(Color.FromRgb(192, 233, 252)); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
转载请注明出处