WPF自定义日历控件Calendar 的方法

news2024/11/24 4:59:06

推荐下载地址 https://www.haolizi.net/example/view_2107.html

<UserControl.Resources>
        <local1:DayConverter x:Key="DayConverter"/><!--导入转换器-->
        <Style x:Key="CalendarStyle1"
                TargetType="{x:Type Calendar}">
            <!--日历控件的背景色,也可以改成绑定的-->
            <Setter Property = "Background"
                     Value="#f6f6f6" />
            <Setter Property = "Template" >
                <Setter.Value >
                    <ControlTemplate TargetType="{x:Type Calendar}">

                        <StackPanel x:Name="PART_Root"
                                     HorizontalAlignment="Center"
                                     VerticalAlignment="Center">
                            <!--这个是日历控件的主体元件,也是内部元件PART_CalendarItem名称不要更改,可以改它的其它样式属性-->
                            <CalendarItem x:Name="PART_CalendarItem"
                                           BorderBrush="{TemplateBinding BorderBrush}"
                                           BorderThickness="{TemplateBinding BorderThickness}"
                                           Background="{TemplateBinding Background}"
                                           Style="{TemplateBinding CalendarItemStyle}"
                                           Height="{TemplateBinding Height}"
                                           Width="{TemplateBinding Width}"
                                           HorizontalAlignment="Stretch"
                                           VerticalAlignment="Stretch" />
                        </StackPanel>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!--日历主体样式表-->
        <Style x:Key="CalendarItemStyle1"
                TargetType="{x:Type CalendarItem}">
            <Setter Property = "Template" >
                <Setter.Value >
                    <ControlTemplate TargetType="{x:Type CalendarItem}">
                        <ControlTemplate.Resources>
                            <DataTemplate x:Key="{x:Static CalendarItem.DayTitleTemplateResourceKey}">
                                <!--日历星期几的绑定样式,我格式化成周一,周二等-->
                                <TextBlock Foreground = "#666666"
                                            FontSize="16"
                                            FontFamily="微软雅黑"
                                            HorizontalAlignment="Center"
                                            Margin="0 15"
                                            Text="{Binding StringFormat=周{0} }"
                                            VerticalAlignment="Center" />
                            </DataTemplate>
                        </ControlTemplate.Resources>
                        <Grid x:Name="PART_Root">
                            <Grid.Resources>
                                <!--设置日历控件 IsEnable = false 时的不可用遮罩层颜色,并且会播放过渡动画-->
                                <SolidColorBrush x:Key="DisabledColor"
                                                  Color="#A5FFFFFF" />
                            </Grid.Resources>


                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal" />
                                    <VisualState x:Name="Disabled">
                                        <!--设置日历控件 IsEnable = false 时遮罩层透明度0-1变色动画-->
                                        <Storyboard>
                                            <DoubleAnimation Duration = "0"
                                                              To="1"
                                                              Storyboard.TargetProperty="Opacity"
                                                              Storyboard.TargetName="PART_DisabledVisual" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>

                            <Border BorderBrush = "#cfcfcf"
                                    BorderThickness="0"
                                    Background="{TemplateBinding Background}"
                                    CornerRadius="2">
                                <Border>
                                    <Grid>
                                        <Grid.Resources>
                                            <!--日历头左箭头按钮样式模版-->
                                            <ControlTemplate x:Key="PreviousButtonTemplate"
                                                             TargetType="{x:Type Button}">
                                                <!--鼠标悬停在左箭头按钮上时改变鼠标指针样式-->
                                                <Grid Cursor = "Hand" >
                                                    <VisualStateManager.VisualStateGroups >
                                                        <VisualStateGroup x:Name="CommonStates">
                                                            <VisualState x:Name="Normal" />
                                                            <VisualState x:Name="MouseOver">
                                                                <!--鼠标悬停在左箭头按钮上时左箭头颜色变化动画-->
                                                                <Storyboard>
                                                                    <ColorAnimation Duration = "0"
                                                                                    To="#FF73A9D8"
                                                                                    Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                                                                    Storyboard.TargetName="path" />
                                                                </Storyboard>
                                                            </VisualState>
                                                            <VisualState x:Name="Disabled">
                                                                <Storyboard>
                                                                    <DoubleAnimation Duration = "0"
                                                                                     To=".5"
                                                                                     Storyboard.TargetProperty="(Shape.Fill).(Brush.Opacity)"
                                                                                     Storyboard.TargetName="path" />
                                                                </Storyboard>
                                                            </VisualState>
                                                        </VisualStateGroup>
                                                    </VisualStateManager.VisualStateGroups>
                                                    <!--左箭头整个区域矩形块-->
                                                    <Rectangle Fill = "#363636"
                                                               Opacity="1"
                                                               Stretch="Fill" />
                                                    <Grid>
                                                        <!--左箭头-->
                                                        <Path x:Name="path"
                                                              Data="M288.75,232.25 L288.75,240.625 L283,236.625 z"
                                                              Fill="#e0e0e0"
                                                              HorizontalAlignment="Left"
                                                              Height="15"
                                                              Width="15"
                                                              Margin="20,0,0,0"
                                                              Stretch="Fill"
                                                              VerticalAlignment="Center" />
                                                    </Grid>
                                                </Grid>
                                            </ControlTemplate>
                                            <!--日历头右箭头按钮样式模版,这块跟左箭头样式模版没什么区别-->
                                            <ControlTemplate x:Key="NextButtonTemplate"
                                                             TargetType="{x:Type Button}">
                                                <Grid Cursor = "Hand" >
                                                    <VisualStateManager.VisualStateGroups >
                                                        <VisualStateGroup x:Name="CommonStates">
                                                            <VisualState x:Name="Normal" />
                                                            <VisualState x:Name="MouseOver">
                                                                <Storyboard>
                                                                    <ColorAnimation Duration = "0"
                                                                                    To="#FF73A9D8"
                                                                                    Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                                                                    Storyboard.TargetName="path" />
                                                                </Storyboard>
                                                            </VisualState>
                                                            <VisualState x:Name="Disabled">
                                                                <Storyboard>
                                                                    <DoubleAnimation Duration = "0"
                                                                                     To=".5"
                                                                                     Storyboard.TargetProperty="(Shape.Fill).(Brush.Opacity)"
                                                                                     Storyboard.TargetName="path" />
                                                                </Storyboard>
                                                            </VisualState>
                                                        </VisualStateGroup>
                                                    </VisualStateManager.VisualStateGroups>
                                                    <Rectangle Fill = "#363636"
                                                               Opacity="1"
                                                               Stretch="Fill" />
                                                    <Grid>
                                                        <Path x:Name="path"
                                                              Data="M282.875,231.875 L282.875,240.375 L288.625,236 z"
                                                              Fill="#e0e0e0"
                                                              HorizontalAlignment="Right"
                                                              Height="15"
                                                              Width="15"
                                                              Margin="0,0,20,0"
                                                              Stretch="Fill"
                                                              VerticalAlignment="Center" />
                                                    </Grid>
                                                </Grid>
                                            </ControlTemplate>
                                            <!--日历头中间年按钮样式模版-->
                                            <ControlTemplate x:Key="HeaderButtonTemplate"
                                                             TargetType="{x:Type Button}">
                                                <Grid Cursor = "Hand" >
                                                    <VisualStateManager.VisualStateGroups >
                                                        <VisualStateGroup x:Name="CommonStates">
                                                            <VisualState x:Name="Normal" />
                                                            <VisualState x:Name="MouseOver">
                                                                <Storyboard>
                                                                    <ColorAnimation Duration = "0"
                                                                                    To="#FF73A9D8"
                                                                                    Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"
                                                                                    Storyboard.TargetName="buttonContent" />
                                                                </Storyboard>
                                                            </VisualState>
                                                            <VisualState x:Name="Disabled">
                                                                <Storyboard>
                                                                    <DoubleAnimation Duration = "0"
                                                                                     To=".5"
                                                                                     Storyboard.TargetProperty="Opacity"
                                                                                     Storyboard.TargetName="buttonContent" />
                                                                </Storyboard>
                                                            </VisualState>
                                                        </VisualStateGroup>
                                                    </VisualStateManager.VisualStateGroups>
                                                    <ContentPresenter x:Name="buttonContent"
                                                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                                                      Content="{TemplateBinding Content}"
                                                                      TextElement.Foreground="#e0e0e0"
                                                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                                      Margin="1,4,1,9"
                                                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                                </Grid>
                                            </ControlTemplate>
                                        </Grid.Resources>

                                        <Grid.RowDefinitions>
                                            <!--日历头,左箭头,,右箭头-->
                                            <RowDefinition Height = "Auto" />
                                            <!--日历内容, 星期几和具体的日期几号几号-->
                                            <RowDefinition Height = "*" />
                                        </Grid.RowDefinitions >
                                        <Grid.ColumnDefinitions >
                                            <!--左箭头-->
                                            <ColumnDefinition Width="Auto" />
                                            <!---->
                                            <ColumnDefinition Width = "*" />
                                            <!--右箭头-->
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <!--,左箭头,,右箭头,整体的背景色-->
                                        <Border Grid.Row="0"
                                                Grid.ColumnSpan= "3"
                                                Background= "#363636" ></Border >
                                        <!--左箭头-->
                                        <Button x:Name= "PART_PreviousButton"
                                                Grid.Column= "0"
                                                Focusable= "False"
                                                HorizontalAlignment= "Left"
                                                Grid.Row= "0"
                                                Template= "{StaticResource PreviousButtonTemplate}" />
                                        <!---->
                                        <Button x:Name= "PART_HeaderButton"
                                                Grid.Column= "1"
                                                FontFamily= "微软雅黑"
                                                Focusable= "False"
                                                FontSize= "26"
                                                HorizontalAlignment= "Center"
                                                Grid.Row= "0"
                                                Template= "{StaticResource HeaderButtonTemplate}"
                                                VerticalAlignment= "Center" />
                                        <!--右箭头-->
                                        <Button x:Name= "PART_NextButton"
                                                Grid.Column= "2"
                                                Focusable= "False"
                                                HorizontalAlignment= "Right"
                                                Grid.Row= "0"
                                                Template= "{StaticResource NextButtonTemplate}" />
                                        <!--日期几号几号内容显示-->
                                        <Border Grid.Row= "1"
                                                Grid.ColumnSpan= "3"
                                                Margin= "0"
                                                BorderBrush= "#cfcfcf"
                                                BorderThickness= "3,0,3,3" >
                                            <Grid x:Name= "PART_MonthView"
                                                  HorizontalAlignment= "Center"
                                                  Visibility= "Visible" >
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width= "*" />
                                                    <ColumnDefinition Width= "*" />
                                                    <ColumnDefinition Width= "*" />
                                                    <ColumnDefinition Width= "*" />
                                                    <ColumnDefinition Width= "*" />
                                                    <ColumnDefinition Width= "*" />
                                                    <ColumnDefinition Width= "*" />
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions >
                                                    <RowDefinition Height= "auto" />
                                                    <RowDefinition Height= "*" />
                                                    <RowDefinition Height= "*" />
                                                    <RowDefinition Height= "*" />
                                                    <RowDefinition Height= "*" />
                                                    <RowDefinition Height= "*" />
                                                    <RowDefinition Height= "*" />
                                                </Grid.RowDefinitions >
                                            </Grid >
                                        </Border >

                                        <!--月和年内容显示-->
                                        <Grid x:Name= "PART_YearView"
                                              Grid.ColumnSpan= "3"
                                              HorizontalAlignment= "Center"
                                              Margin= "6,-3,7,6"
                                              Grid.Row= "1"
                                              Visibility= "Hidden"
                                              VerticalAlignment= "Center" >
                                            <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 >
                            </Border >
                            <!--日历不可用的遮罩层-->
                            <Rectangle x:Name= "PART_DisabledVisual"
                                       Fill= "{StaticResource DisabledColor}"
                                       Opacity= "0"
                                       RadiusY= "2"
                                       RadiusX= "2"
                                       Stretch= "Fill"
                                       Stroke= "{StaticResource DisabledColor}"
                                       StrokeThickness= "1"
                                       Visibility= "Collapsed" />
                        </Grid >
                        <!--触发器属性-->
                        <ControlTemplate.Triggers >
                            <Trigger Property= "IsEnabled"
                                     Value= "False" >
                                <Setter Property= "Visibility"
                                        TargetName= "PART_DisabledVisual"
                                        Value= "Visible" />
                            </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 >
                            <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= "CalendarDayButtonStyle1"
               TargetType= "{x:Type CalendarDayButton}" >
            <Setter Property= "Margin"
                    Value= "1" />
            <Setter Property= "MinWidth"
                    Value= "5" />
            <Setter Property= "MinHeight"
                    Value= "5" />
            <Setter Property= "FontSize"
                    Value= "22" />
            <Setter Property= "FontFamily"
                    Value= "微软雅黑" />
            <Setter Property= "HorizontalContentAlignment"
                    Value= "Center" />
            <Setter Property= "VerticalContentAlignment"
                    Value= "Center" />
            <Setter Property= "Template" >
                <Setter.Value >
                    <ControlTemplate TargetType= "{x:Type CalendarDayButton}" > 
                            <Grid >
                            <VisualStateManager.VisualStateGroups >
                                <VisualStateGroup x:Name= "CommonStates" >
                                    <VisualStateGroup.Transitions >
                                        <VisualTransition GeneratedDuration= "0:0:0.1" />
                                    </VisualStateGroup.Transitions >
                                    <VisualState x:Name= "Normal" />
                                    <!--悬停的颜色动画-->
                                    <VisualState x:Name= "MouseOver" >
                                        <Storyboard >
                                            <DoubleAnimation Duration= "0"
                                                             To= "0.5"
                                                             Storyboard.TargetProperty= "Opacity"
                                                             Storyboard.TargetName= "HighlightBackground" />
                                        </Storyboard>
                                    </VisualState >
                                    <!--按下后动画-->
                                    <VisualState x:Name= "Pressed" >
                                        <Storyboard >
                                            <DoubleAnimation Duration= "0"
                                                             To= "0.5"
                                                             Storyboard.TargetProperty= "Opacity"
                                                             Storyboard.TargetName= "HighlightBackground" />
                                        </Storyboard>
                                    </VisualState >
                                    <!--不可用动画-->
                                    <VisualState x:Name= "Disabled" >
                                        <Storyboard >
                                            <DoubleAnimation Duration= "0"
                                                             To= "0"
                                                             Storyboard.TargetProperty= "Opacity"
                                                             Storyboard.TargetName= "HighlightBackground" />
                                            <DoubleAnimation Duration= "0"
                                                             To= ".35"
                                                             Storyboard.TargetProperty= "Opacity"
                                                             Storyboard.TargetName= "NormalText" />
                                        </Storyboard>
                                    </VisualState >
                                </VisualStateGroup >
                                <VisualStateGroup x:Name= "SelectionStates" >
                                    <VisualStateGroup.Transitions >
                                        <VisualTransition GeneratedDuration= "0" />
                                    </VisualStateGroup.Transitions >
                                    <VisualState x:Name= "Unselected" />
                                    <!--选中某日期的样式-->
                                    <VisualState x:Name= "Selected" >
                                        <Storyboard >
                                            <DoubleAnimation Duration= "0"
                                                             To= ".75"
                                                             Storyboard.TargetProperty= "Opacity"
                                                             Storyboard.TargetName= "SelectedBackground" />
                                            <ColorAnimation Duration= "0"
                                                            To= "white"
                                                            Storyboard.TargetProperty= "(TextElement.Foreground).(SolidColorBrush.Color)"
                                                            Storyboard.TargetName= "NormalText" />
                                        </Storyboard>
                                    </VisualState >
                                </VisualStateGroup >
                                <VisualStateGroup x:Name= "CalendarButtonFocusStates" >
                                    <VisualStateGroup.Transitions >
                                        <VisualTransition GeneratedDuration= "0" />
                                    </VisualStateGroup.Transitions >
                                    <VisualState x:Name= "CalendarButtonFocused" >
                                        <Storyboard >
                                            <ObjectAnimationUsingKeyFrames Duration= "0"
                                                                           Storyboard.TargetProperty= "Visibility"
                                                                           Storyboard.TargetName= "DayButtonFocusVisual" >
                                                <DiscreteObjectKeyFrame KeyTime= "0" >
                                                    <DiscreteObjectKeyFrame.Value >
                                                        <Visibility >Visible</Visibility >
                                                    </DiscreteObjectKeyFrame.Value >
                                                </DiscreteObjectKeyFrame >
                                            </ObjectAnimationUsingKeyFrames >
                                        </Storyboard>
                                    </VisualState >
                                    <VisualState x:Name= "CalendarButtonUnfocused" >
                                        <Storyboard >
                                            <ObjectAnimationUsingKeyFrames Duration= "0"
                                                                           Storyboard.TargetProperty= "Visibility"
                                                                           Storyboard.TargetName= "DayButtonFocusVisual" >
                                                <DiscreteObjectKeyFrame KeyTime= "0" >
                                                    <DiscreteObjectKeyFrame.Value >
                                                        <Visibility >Collapsed</Visibility >
                                                    </DiscreteObjectKeyFrame.Value >
                                                </DiscreteObjectKeyFrame >
                                            </ObjectAnimationUsingKeyFrames >
                                        </Storyboard>
                                    </VisualState >
                                </VisualStateGroup >
                                <VisualStateGroup x:Name= "ActiveStates" >
                                    <VisualStateGroup.Transitions >
                                        <VisualTransition GeneratedDuration= "0" />
                                    </VisualStateGroup.Transitions >
                                    <VisualState x:Name= "Active" />
                                    <VisualState x:Name= "Inactive" >
                                        <Storyboard >
                                            <ColorAnimation Duration= "0"
                                                            To= "#b4b3b3"
                                                            Storyboard.TargetProperty= "(TextElement.Foreground).(SolidColorBrush.Color)"
                                                            Storyboard.TargetName= "NormalText" />
                                        </Storyboard>
                                    </VisualState >
                                </VisualStateGroup >
                                <VisualStateGroup x:Name= "DayStates" >
                                    <VisualStateGroup.Transitions >
                                        <VisualTransition GeneratedDuration= "0" />
                                    </VisualStateGroup.Transitions >
                                    <VisualState x:Name= "RegularDay" />
                                    <!--今天的样式-->
                                    <VisualState x:Name= "Today" >
                                        <Storyboard >
                                            <DoubleAnimation Duration= "0"
                                                             To= "1"
                                                             Storyboard.TargetProperty= "Opacity"
                                                             Storyboard.TargetName= "TodayBackground" />
                                            <ColorAnimation Duration= "0"
                                                            To= "#666666"
                                                            Storyboard.TargetProperty= "(TextElement.Foreground).(SolidColorBrush.Color)"
                                                            Storyboard.TargetName= "NormalText" />

                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty= "Visibility"
                                                                           Storyboard.TargetName= "imgToday" >
                                                <DiscreteObjectKeyFrame KeyTime= "0" >
                                                    <DiscreteObjectKeyFrame.Value >
                                                        <Visibility >Visible</Visibility >
                                                    </DiscreteObjectKeyFrame.Value >
                                                </DiscreteObjectKeyFrame >
                                            </ObjectAnimationUsingKeyFrames >

                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <!--过期日期的-->
                                <VisualStateGroup x:Name= "BlackoutDayStates" >
                                    <VisualStateGroup.Transitions >
                                        <VisualTransition GeneratedDuration= "0" />
                                    </VisualStateGroup.Transitions >
                                    <VisualState x:Name= "NormalDay" />
                                    <VisualState x:Name= "BlackoutDay" >
                                        <Storyboard >
                                            <DoubleAnimation Duration= "0"
                                                             To= ".2"
                                                             Storyboard.TargetProperty= "Opacity"
                                                             Storyboard.TargetName= "Blackout" />
                                        </Storyboard>
                                    </VisualState >
                                </VisualStateGroup >
                            </VisualStateManager.VisualStateGroups >
                            <Border BorderBrush= "#bbbbbb"
                                    BorderThickness= "1" >
                                <Border BorderBrush= "white"
                                        BorderThickness= "2,2,0,0"
                                        Margin= "1,1,0,0" ></Border >
                            </Border >

                            <Rectangle x:Name= "TodayBackground"
                                       Fill= "#c6c6c6"
                                       Opacity= "0"
                                       RadiusY= "1"
                                       RadiusX= "1" />
                            <Rectangle x:Name= "SelectedBackground"
                                       Fill= "#6eafbf"
                                       Opacity= "0"
                                       RadiusY= "1"
                                       RadiusX= "1" />
                            <Border BorderBrush= "{TemplateBinding BorderBrush}"
                                    BorderThickness= "{TemplateBinding BorderThickness}"
                                    Background= "{TemplateBinding Background}" />
                            <Rectangle x:Name= "HighlightBackground"
                                       Fill= "#FFBADDE9"
                                       Opacity= "0"
                                       RadiusY= "1"
                                       RadiusX= "1" />
                            <ContentPresenter x:Name= "NormalText"
                                              TextElement.Foreground= "#666666"
                                              HorizontalAlignment= "{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment= "{TemplateBinding VerticalContentAlignment}" />
                            <Path x:Name= "Blackout"
                                  Data= "M8.1772461,11.029181 L10.433105,11.029181 L11.700684,12.801641 L12.973633,11.029181 L15.191895,11.029181 L12.844727,13.999395 L15.21875,17.060919 L12.962891,17.060919 L11.673828,15.256231 L10.352539,17.060919 L8.1396484,17.060919 L10.519043,14.042364 z"
                                  Fill= "#FF000000"
                                  HorizontalAlignment= "Stretch"
                                  Margin= "3"
                                  Opacity= "0"
                                  RenderTransformOrigin= "0.5,0.5"
                                  Stretch= "Fill"
                                  VerticalAlignment= "Stretch" />
                            <Rectangle x:Name= "DayButtonFocusVisual"
                                       IsHitTestVisible= "false"
                                       RadiusY= "1"
                                       RadiusX= "1"
                                       Stroke= "#FF45D6FA"
                                       Visibility= "Collapsed" />
                            <!--Width= "44"
                                   Height= "34"-->
                            <Image x:Name= "imgToday" 
                                   Source= "../../CommonInMy/Image/秋意图.jpg"
                                   VerticalAlignment= "Top"
                                   HorizontalAlignment= "Left"
                                   Visibility= "Hidden" />
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <TextBlock FontSize="10" Grid.Row="4"  Foreground="DarkGray"  Text="{Binding Converter={StaticResource DayConverter} }">  </TextBlock>
                                  
                            </Grid> 
                        </Grid>
                        
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key= "CalendarButtonStyle1"
               TargetType= "{x:Type CalendarButton}" >
            <Setter Property= "Background"
                    Value= "#FFBADDE9" />
            <Setter Property= "MinWidth"
                    Value= "80" />
            <Setter Property= "MinHeight"
                    Value= "120" />
            <Setter Property= "Margin"
                    Value= "20" />
            <Setter Property= "FontSize"
                    Value= "25" />
            <Setter Property= "HorizontalContentAlignment"
                    Value= "Center" />
            <Setter Property= "VerticalContentAlignment"
                    Value= "Center" />
            <Setter Property= "Template" >
                <Setter.Value >
                    <ControlTemplate TargetType= "{x:Type CalendarButton}" >
                        <Grid >
                            <VisualStateManager.VisualStateGroups >
                                <VisualStateGroup x:Name= "CommonStates" >
                                    <VisualStateGroup.Transitions >
                                        <VisualTransition GeneratedDuration= "0:0:0.1" />
                                    </VisualStateGroup.Transitions >
                                    <VisualState x:Name= "Normal" />
                                    <VisualState x:Name= "MouseOver" >
                                        <Storyboard >
                                            <DoubleAnimation Duration= "0"
                                                             To= ".5"
                                                             Storyboard.TargetProperty= "Opacity"
                                                             Storyboard.TargetName= "Background" />
                                        </Storyboard>
                                    </VisualState >
                                    <VisualState x:Name= "Pressed" >
                                        <Storyboard >
                                            <DoubleAnimation Duration= "0"
                                                             To= ".5"
                                                             Storyboard.TargetProperty= "Opacity"
                                                             Storyboard.TargetName= "Background" />
                                        </Storyboard>
                                    </VisualState >
                                </VisualStateGroup >
                                <VisualStateGroup x:Name= "SelectionStates" >
                                    <VisualStateGroup.Transitions >
                                        <VisualTransition GeneratedDuration= "0" />
                                    </VisualStateGroup.Transitions >
                                    <VisualState x:Name= "Unselected" />
                                    <VisualState x:Name= "Selected" >
                                        <Storyboard >
                                            <DoubleAnimation Duration= "0"
                                                             To= ".75"
                                                             Storyboard.TargetProperty= "Opacity"
                                                             Storyboard.TargetName= "SelectedBackground" />
                                        </Storyboard>
                                    </VisualState >
                                </VisualStateGroup >
                                <VisualStateGroup x:Name= "ActiveStates" >
                                    <VisualStateGroup.Transitions >
                                        <VisualTransition GeneratedDuration= "0" />
                                    </VisualStateGroup.Transitions >
                                    <VisualState x:Name= "Active" />
                                    <VisualState x:Name= "Inactive" >
                                        <Storyboard >
                                            <ColorAnimation Duration= "0"
                                                            To= "#FF777777"
                                                            Storyboard.TargetProperty= "(TextElement.Foreground).(SolidColorBrush.Color)"
                                                            Storyboard.TargetName= "NormalText" />
                                        </Storyboard>
                                    </VisualState >
                                </VisualStateGroup >
                                <VisualStateGroup x:Name= "CalendarButtonFocusStates" >
                                    <VisualStateGroup.Transitions >
                                        <VisualTransition GeneratedDuration= "0" />
                                    </VisualStateGroup.Transitions >
                                    <VisualState x:Name= "CalendarButtonFocused" >
                                        <Storyboard >
                                            <ObjectAnimationUsingKeyFrames Duration= "0"
                                                                           Storyboard.TargetProperty= "Visibility"
                                                                           Storyboard.TargetName= "CalendarButtonFocusVisual" >
                                                <DiscreteObjectKeyFrame KeyTime= "0" >
                                                    <DiscreteObjectKeyFrame.Value >
                                                        <Visibility >Visible</Visibility >
                                                    </DiscreteObjectKeyFrame.Value >
                                                </DiscreteObjectKeyFrame >
                                            </ObjectAnimationUsingKeyFrames >
                                        </Storyboard>
                                    </VisualState >
                                    <VisualState x:Name= "CalendarButtonUnfocused" >
                                        <Storyboard >
                                            <ObjectAnimationUsingKeyFrames Duration= "0"
                                                                           Storyboard.TargetProperty= "Visibility"
                                                                           Storyboard.TargetName= "CalendarButtonFocusVisual" >
                                                <DiscreteObjectKeyFrame KeyTime= "0" >
                                                    <DiscreteObjectKeyFrame.Value >
                                                        <Visibility >Collapsed</Visibility >
                                                    </DiscreteObjectKeyFrame.Value >
                                                </DiscreteObjectKeyFrame >
                                            </ObjectAnimationUsingKeyFrames >
                                        </Storyboard>
                                    </VisualState >
                                </VisualStateGroup >
                            </VisualStateManager.VisualStateGroups >
                            <Rectangle x:Name= "SelectedBackground"
                                       Fill= "{TemplateBinding Background}"
                                       Opacity= "0"
                                       RadiusY= "1"
                                       RadiusX= "1" />
                            <Rectangle x:Name= "Background"
                                       Fill= "{TemplateBinding Background}"
                                       Opacity= "0"
                                       RadiusY= "1"
                                       RadiusX= "1" />
                            <ContentPresenter x:Name= "NormalText"
                                              TextElement.Foreground= "#FF333333"
                                              HorizontalAlignment= "{TemplateBinding HorizontalContentAlignment}"
                                              Margin= "1,0,1,1"
                                              VerticalAlignment= "{TemplateBinding VerticalContentAlignment}" />
                            <Rectangle x:Name= "CalendarButtonFocusVisual"
                                       IsHitTestVisible= "false"
                                       RadiusY= "1"
                                       RadiusX= "1"
                                       Stroke= "#FF45D6FA"
                                       Visibility= "Collapsed" />
                        </Grid >
                        <ControlTemplate.Triggers >
                            <Trigger Property= "IsFocused"
                                     Value= "True" >
                                <Setter Property= "Visibility"
                                        TargetName= "CalendarButtonFocusVisual"
                                        Value= "Visible" />
                            </Trigger >
                        </ControlTemplate.Triggers >
                    </ControlTemplate >
                </Setter.Value >
            </Setter >
        </Style >
    </UserControl.Resources >
    <Grid>
        <Calendar Style= "{DynamicResource CalendarStyle1}"
                  CalendarItemStyle= "{DynamicResource CalendarItemStyle1}"
                  CalendarDayButtonStyle= "{DynamicResource CalendarDayButtonStyle1}"
                  CalendarButtonStyle= "{DynamicResource CalendarButtonStyle1}">

        </Calendar>
    </Grid>

其中绝大部分功能都是写样式:因为没有绑定的 itemsource 功能,加入一个textblock 来展示操作:

<Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <TextBlock FontSize="10" Grid.Row="4"  Foreground="DarkGray"  Text="{Binding Converter={StaticResource DayConverter} }">  </TextBlock>
              
        </Grid>  

只能够用转换器来操作,因为这converter 调用的时候是自动value传入的是 当前 日期模式 ‘2024-10-24 00:00:00’ 需要再转换器 先 转换成日期操作,然后进入数据库查询当日数据 然后返回 需要字段。
在这里插入图片描述
所以转换的方法应该这么写:

  public  class DayConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                DateTime test = (DateTime)value;
                string date = test.ToString("yyyy-MM-dd");
                string wheres = "";
                if (GModel.User.SimiaoId!=1 )
                {
                    wheres += $" and SiniaoId = {GModel.User.SimiaoId} ";
                }
                wheres += $" and ThisDay = '{date}' ";
                string Outstr = new DAL.DALFahuiInfo().QueryShowByCalender(wheres);
                return Outstr;
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion


    }

其中关于日期的sql操作是这样的 新增一个日期列,然后比对日期即可

 /// <summary>
        /// 显示当前日期数据
        /// </summary>
        /// <param name="wheres">wheres条件</param>
        /// <returns></returns>
        public string  QueryShowByCalender(string Wheres)
        { 
            string sql = $@"SELECT b.FahuiName   FROM  ( select a.* from (select   FahuiName ,CONVERT(varchar(100),SetStartDate, 23) as  ThisDay  from [FahuiInfo]) a     where 1=1 {Wheres}) b";
             IEnumerable<string> ThisFanhuiNames =  DapperDbHelper.Query<string>(sql);
            string OutStr = "";
            if (ThisFanhuiNames != null)
            {
                OutStr = string.Join(",",ThisFanhuiNames);
            }
            return OutStr;
        }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2229878.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

《ToDesk云电脑vs青椒云性能测试,谁更能实现游戏自由?》

ToDesk云电脑vs青椒云性能测试 【前言】【使用云电脑的意义】【实测软件】【性能参数对比】1. 硬件配置2.游戏兼容性3. 延迟、流畅度与画面清晰度4. 用户体验5. 价格对比6. 附加功能 【游戏性能测试】《游戏一 黑悟空》《游戏二 赛博朋克 2077》《游戏三 CS反恐精英》 【本文小…

ctfshow文件包含web78~81

目录 web78 方法一&#xff1a;filter伪协议 方法二&#xff1a;input协议 方法三&#xff1a;data协议 web79 方法一:input协议 方法二&#xff1a;data协议 web80 方法一&#xff1a;input协议 方法二&#xff1a;日志包含getshell web81 web78 if(isset($_GET[file]…

动态IP是什么?

随着互联网成为人们生活的重要组成部分&#xff0c;以信息传递为主导的时代种&#xff0c;网络连接质量对我们的工作效率、学习进度以及娱乐体验等方面都有很大影响。 动态IP&#xff0c;作为网络连接中的一种重要IP代理形式&#xff0c;越来越受到用户的欢迎。本文将深入解析…

vue下载安装

目录 vue工具前置要求&#xff1a;安装node.js并配置好国内镜像源下载安装 vue 工具 系统&#xff1a;Windows 11 前置要求&#xff1a;安装node.js并配置好国内镜像源 参考&#xff1a;本人写的《node.js下载、安装、设置国内镜像源&#xff08;永久&#xff09;&#xff…

在JAVA中使用Paho MQTT客户端

1.在maven里面配置好依赖 <dependency><groupId>org.eclipse.paho</groupId><artifactId>org.eclipse.paho.client.mqttv3</artifactId><version>1.2.2</version> </dependency> 2.创建APP类 package com.leo;import org.e…

Django+websocket实现一个简单聊天

WebSocket是一种在单个TCP连接上进行全双工通信的协议。它由IETF在2011年定为标准RFC 6455&#xff0c;并由RFC7936补充规范&#xff0c;同时WebSocket API也被W3C定为标准。 1、定义与原理 WebSocket是独立的、创建在TCP上的协议&#xff0c;它使用HTTP/1.1协议的101状态码进…

MATLAB实现人类学习优化算法HLO

1.算法简介 人类学习优化算法&#xff08;Human Learning-based Optimization&#xff0c;HLO&#xff09;是一种基于人类学习过程开发的启发式算法。HLO算法的设计灵感来源于人类的智慧和经验&#xff0c;特别是人类在学习和调整过程中展现出的适应性、学习能力和创新思维。该…

【果蔬识别】Python+卷积神经网络算法+深度学习+人工智能+机器学习+TensorFlow+计算机课设项目+算法模型

一、介绍 果蔬识别系统&#xff0c;本系统使用Python作为主要开发语言&#xff0c;通过收集了12种常见的水果和蔬菜&#xff08;‘土豆’, ‘圣女果’, ‘大白菜’, ‘大葱’, ‘梨’, ‘胡萝卜’, ‘芒果’, ‘苹果’, ‘西红柿’, ‘韭菜’, ‘香蕉’, ‘黄瓜’&#xff09;…

Android 策略设计模式的使用:使用设计模式,减少烂代码,让项目更好维护

目录 大家好呀~&#xff0c;我是前期后期&#xff0c;在网上冲浪的一名程序员&#xff0c;分享一些自己学到的知识&#xff0c;希望对大家有所帮助 前言&#xff1a;为什么要使用设计模式 在项目开发过程中&#xff0c;我们会对接很多种支付&#xff1a;国内&#xff08;微信…

uniapp和vite项目配置多环境编译,增加测试环境变量配置--mode test

如果你的项目是使用vite和uniapp配置开发的&#xff0c;就可以在代码里面获取到这些变量&#xff0c;但是开发&#xff0c;测试和发布是不同的请求地址&#xff0c;所以需要配置。Vite 使用 dotenv 从你的 环境目录 中的下列文件加载额外的环境变量&#xff1a; .env …

动态规划 - 编辑距离

115. 不同的子序列 困难 给你两个字符串 s 和 t &#xff0c;统计并返回在 s 的 子序列 中 t 出现的个数&#xff0c;结果需要对 10^9 7 取模。 算法思想&#xff1a;利用动态规划&#xff0c;分s[i - 1] 与 t[j - 1]相等&#xff0c;s[i - 1] 与 t[j - 1] 不相等两种情况具…

sudo apt install jupyter-notebook安装notebook失败E: Aborting install.

问题&#xff1a; sudo apt install jupyter-notebook安装notebook失败E: Aborting install. ~/jie/mywork/PointNetCFD$ sudo apt install jupyter-notebook --fix-missing Reading package lists... Done Building dependency tree Reading state information... Do…

第16课 核心函数(方法)

掌握常用的内置函数及其用法。 数学类函数&#xff1a;abs、divmod、max、min、pow、round、sum。 类型转换函数&#xff1a;bool、int、float、str、ord、chr、bin、hex、tuple、list、dict、set、enumerate、range、object。 序列操作函数&#xff1a;all、any、filter、m…

【1个月速成Java】基于Android平台开发个人记账app学习日记——第2天,启动项目

24.11.01 下面讲一下如何通过USB连接手机然后启动app实现真机测试&#xff0c;还是有一些坑的。 调整电脑的驱动程序&#xff0c;完成USB的连接 在启动项目的第一步我就遇见了问题&#xff0c;那就是插入usb线以后没有检测到设备。想要完成连接需要2个步骤&#xff0c;第一步…

使用Mac如何才能提高OCR与翻译的效率

OCR与截图大家都不陌生&#xff0c;或许有的朋友对于这两项功能用到的不多&#xff0c;但是如果经常会用到的话&#xff0c;那你就该看看了 iOCR&#xff0c;快捷键唤出翻译窗口&#xff0c;不论是截图翻译、划词翻译、输入翻译、剪切板翻译&#xff0c;统统快捷键完成&#x…

Etsy又被封号了!这次我终于搞懂了原因...

你是否真的了解在Etsy开店有哪些红线不能踩&#xff1f;你是否真的知道Etsy被封号后如何解决&#xff1f;本文我将探讨Etsy账号被封的常见原因&#xff0c;以及卖家可以采取的应对策略&#xff0c;以期减轻对跨境业务的伤害程度&#xff0c;感兴趣的商家速速码住&#xff0c;不…

MySQL — 事务 (o゚▽゚)o

文本目录&#xff1a; ❄️一、什么是事务&#xff1a; ❄️二、ACID特性&#xff1a; ❄️三、使用事务&#xff1a; ▶1、查看支持事务的存储引擎&#xff1a; ▶2、语法&#xff1a; ▶3、开启并且回滚&#xff1a; ▶4、开启并且提交&#xff1a; ▶ 5、保存点&#xff…

DOS时代软件遗憾落幕,国产编程新势力接过火炬

在计算机发展史上&#xff0c;DOS时代是一个不可磨灭的篇章。那个时期&#xff0c;虽然操作系统的图形界面尚未普及&#xff0c;但一款款经典软件却为我们打开了通往数字世界的大门&#xff0c;让我们在那个相对简单却充满魅力的时代中&#xff0c;感受到科技的魅力与创新的力量…

Qt的信号槽机制学习一

一、Qt理论知识简记 &#xff08;一&#xff09;信号与槽[1] 信号与槽是Qt编程的基础&#xff0c;其使得处理界面上各个组件的交互操作变得比较直观和简单&#xff0c;GUI&#xff08;Graphical User Interface&#xff09;程序设计的主要工作就是对界面上各组件的信号进行相应…

P11232 [CSP-S 2024] 超速检测

P11232 [CSP-S 2024] 超速检测 难度&#xff1a;普及/提高。 考点&#xff1a;二分、贪心。 题意&#xff1a; 题意较长&#xff0c;没有题目大意&#xff0c;否则你也大意。 主干道长度为 L L L&#xff0c;有 n n n 辆车&#xff0c;看做左端点为 0 0 0&#xff0c;第 …