WPF Calendar样式

原生的不可改变大小 这样写可以改变样式。

<!--Day按钮样式-->
    <Style x:Key="CalendarDayButtonStyle" BasedOn="{StaticResource XXX}" TargetType="{x:Type CalendarDayButton}">
        <Setter Property="MinWidth" Value="100" />
        <Setter Property="MinHeight" Value="80" />
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="FontFamily" Value="微软雅黑" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Foreground" Value="Red" />
        <Setter Property="Margin" Value="2" />
        <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}" BorderBrush="Red"  BorderThickness="1" Margin="0,1,1,0"/>
                        <ContentPresenter x:Name="NormalText" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          Margin="0" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          TextElement.Foreground="{TemplateBinding Foreground}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Red"></Setter>
                            <Setter Property="Foreground" Value="White"></Setter>
                        </Trigger>
                        <Trigger Property="IsToday" Value="True">
                            <Setter Property="Background" Value="Red"></Setter>
                            <Setter Property="Foreground" Value="Black"></Setter>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Red"></Setter>
                            <Setter Property="Foreground" Value="Green"></Setter>
                        </Trigger>
                        <!--不可用日期-->
                        <Trigger Property="IsBlackedOut" Value="True">
                            <Setter Property="Opacity" Value="0.5" TargetName="Grid"></Setter>
                            <Setter Property="Background" Value="LightGray"/>
                        </Trigger>
                        <!--不在当月的日期-->
                        <Trigger Property="IsInactive" Value="True">
                            <Setter Property="Opacity" Value="0.3" TargetName="Grid"></Setter>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Opacity" Value="0.6" TargetName="Grid"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--日历日期面板样式-->
    <Style x:Key="CalendarItemStyle" TargetType="{x:Type CalendarItem}">
        <Setter Property="Margin" Value="1" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CalendarItem}">
                    <ControlTemplate.Resources>
                        <!-- 头部星期样式-->
                        <DataTemplate x:Key="{x:Static CalendarItem.DayTitleTemplateResourceKey}">
                            <TextBlock Text="{Binding StringFormat=周{0}}" FontWeight="Bold" FontFamily="微软雅黑"  Foreground="Red"
                                       FontSize="14" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5" Opacity="0.8"/>
                        </DataTemplate>
                    </ControlTemplate.Resources>
                    <Grid x:Name="PART_Root">
                        <Border BorderBrush="Red" BorderThickness="1" Background="Green" Margin="5">
                            <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="White">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="0.8*"/>
                                        <ColumnDefinition Width="auto"/>
                                        <ColumnDefinition Width="auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Button x:Name="PART_PreviousButton" HorizontalAlignment="Right"  Style="{DynamicResource       Red}" Focusable="False" Grid.Column="0" FontSize="12" Content="&lt;" />
                                    <Button x:Name="PART_HeaderButton" HorizontalAlignment="Center" Content="今天" Style="{DynamicResource ButtonCalendar}" Focusable="False" Grid.Column="1"/>
                                    <Button x:Name="PART_NextButton" HorizontalAlignment="Left" Style="{DynamicResource ButtonCalendar}" Focusable="False" Grid.Column="2" FontSize="12" Content="&gt;" />
                                </Grid>
                                <!--PART_MonthView 月样式 131-->
                                <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 月样式 112-->
                                <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.8"/>
                        </Trigger>
                        <DataTrigger Binding="{Binding DisplayMode,RelativeSource={RelativeSource  Mode=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 Mode=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="100" />
        <Setter Property="MinHeight" Value="100"/>
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="FontFamily" Value="微软雅黑"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" 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" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          TextElement.Foreground="{DynamicResource PrimaryTextBrush}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="true">
                            <Setter Property="Background" Value="Red"/>
                            <Setter Property="Foreground" Value="Blue"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Red"/>
                            <Setter Property="Foreground" Value="Blue"/>
                        </Trigger>
                        <!--不在当月的日期-->
                        <Trigger Property="IsInactive" Value="True">
                            <Setter Property="Opacity" Value="0.8" TargetName="Grid"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Opacity" Value="0.5" TargetName="Grid"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--默认日历样式-->
    <Style x:Key="CalendarDefaultStyle" TargetType="{x:Type Calendar}">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Foreground" Value="Red"/>
        <Setter Property="CalendarButtonStyle" Value="{StaticResource CalendarButtonStyle}"/>
        <Setter Property="CalendarDayButtonStyle" Value="{StaticResource CalendarDayButtonStyle}"/>
        <Setter Property="CalendarItemStyle" Value="Green"/>
        <Setter Property="Background" Value="Blue"/>
        <Setter Property="BorderBrush" Value="Red" />
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="FontSize" Value="12"/>
        <Setter Property="FontFamily" Value="微软雅黑"/>
        <Setter Property="IsTodayHighlighted" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Calendar}">
                    <StackPanel x:Name="PART_Root" HorizontalAlignment="Center" Background="Transparent">
                        <CalendarItem x:Name="PART_CalendarItem" BorderBrush="{TemplateBinding BorderBrush}" FontSize="14"
                                      FontFamily="微软雅黑" BorderThickness="2" Background="Red" 
                                      Style="{TemplateBinding CalendarItemStyle}"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值