WPF-自定义Calendar样式,动态切换中英文并自定义文字

本文主要实现以下两个功能(完整代码在最下面):

自定义Calendar样式,重写CalendarItemStyle、CalendarDayButtonStyle、CalendarButtonStyle

中英文切换时,通过转换器改变日历中的内容

最终效果:

 

CalendarItemStyle、CalendarDayButtonStyle、CalendarButtonStyle

<DropShadowEffect x:Key="ShadowEffectS03" Direction="270" Color="#999999" ShadowDepth="2" BlurRadius="20" Opacity="0.2" x:Shared="false"/>
<converter:CalendarLangToConverter x:Key="CalendarLangToConverter"></converter:CalendarLangToConverter>

<Style x:Key="CommonCalendarButtonStyle" TargetType="{x:Type CalendarButton}">
    <Setter Property="Background" Value="#6B57C7"/>
    <Setter Property="Width" Value="66"/>
    <Setter Property="Height" Value="66"/>
    <Setter Property="FontSize" Value="10"/>
    <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" Storyboard.TargetName="Background" To="0.5" Storyboard.TargetProperty="Opacity"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="Background" To=".5" Storyboard.TargetProperty="Opacity"/>
                                </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" Storyboard.TargetName="SelectedBackground" To=".75" Storyboard.TargetProperty="Opacity"/>
                                    <ColorAnimation Duration="0"
                                                    To="#FFFFFFFF"
                                                    Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"
                                                    Storyboard.TargetName="NormalText" />
                                </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" Storyboard.TargetName="NormalText" To="#999999" Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="CalendarButtonFocusStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0"/>
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="CalendarButtonFocused">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="CalendarButtonFocusVisual" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="CalendarButtonUnfocused">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="CalendarButtonFocusVisual" Storyboard.TargetProperty="Visibility">
                                        <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" RadiusX="10" RadiusY="10"/>
                    <Rectangle x:Name="Background" Fill="{TemplateBinding Background}" Opacity="0" RadiusX="10" RadiusY="10"/>
                    <ContentPresenter x:Name="NormalText" TextElement.FontSize="14" TextElement.Foreground="#4D5E80" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1,0,1,1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                        <ContentPresenter.Content>
                            <MultiBinding Converter="{StaticResource CalendarLangToConverter}" ConverterParameter="YearMonth">
                                <Binding Path="Content" RelativeSource="{RelativeSource TemplatedParent}" UpdateSourceTrigger="PropertyChanged"/>
                                <Binding Path="DataContext.Lang" RelativeSource="{RelativeSource AncestorType=Window}" UpdateSourceTrigger="PropertyChanged"/>
                            </MultiBinding>
                        </ContentPresenter.Content>
                    </ContentPresenter>
                    <Rectangle x:Name="CalendarButtonFocusVisual" IsHitTestVisible="false" RadiusX="10" RadiusY="10" Stroke="Transparent" 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>

<Style x:Key="CommonCalendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}">
    <Setter Property="Width" Value="32"/>
    <Setter Property="Height" Value="32"/>
    <Setter Property="MinWidth" Value="5"/>
    <Setter Property="MinHeight" Value="5"/>
    <Setter Property="FontSize" Value="10"/>
    <Setter Property="Cursor" Value="Hand"/>
    <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" Storyboard.TargetName="HighlightBackground" To="0.5" Storyboard.TargetProperty="Opacity"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="HighlightBackground" To="0.5" Storyboard.TargetProperty="Opacity"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="HighlightBackground" To="0" Storyboard.TargetProperty="Opacity"/>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="NormalText" To=".35" Storyboard.TargetProperty="Opacity"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0"/>
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Unselected"/>
                            <VisualState x:Name="Selected">
                                <Storyboard>
                                    <ColorAnimation Duration="0" Storyboard.TargetName="NormalText" To="#FFFFFFFF" Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"/>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="SelectedBackground" To=".75" Storyboard.TargetProperty="Opacity"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="CalendarButtonFocusStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0"/>
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="CalendarButtonFocused">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="DayButtonFocusVisual" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="CalendarButtonUnfocused">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="DayButtonFocusVisual" Storyboard.TargetProperty="Visibility">
                                        <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" Storyboard.TargetName="NormalText" To="#999999" Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"/>
                                </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" Storyboard.TargetName="TodayBackground" To="1" Storyboard.TargetProperty="Opacity"/>
                                    <ColorAnimation Duration="0" Storyboard.TargetName="NormalText" To="#FFFFFFFF" Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"/>
                                </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" Storyboard.TargetName="Blackout" To=".2" Storyboard.TargetProperty="Opacity"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Rectangle x:Name="TodayBackground" Fill="#FFAAAAAA" Opacity="0" RadiusX="10" RadiusY="10"/>
                    <Rectangle x:Name="SelectedBackground" Fill="#6B57C7" Opacity="0" RadiusX="10" RadiusY="10"/>
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                    <Rectangle x:Name="HighlightBackground" Fill="#6B57C7" Opacity="0" RadiusX="10" RadiusY="10"/>
                    <ContentPresenter x:Name="NormalText" TextElement.FontSize="14" Content="{TemplateBinding Content}" TextElement.Foreground="#4D5E80" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="5,1,5,1" 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" RadiusX="10" RadiusY="10" Stroke="Transparent" Visibility="Collapsed"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="CommonCalendarItemStyle" TargetType="{x:Type CalendarItem}">
    <Setter Property="Margin" Value="10,25,10,13"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CalendarItem}">
                <ControlTemplate.Resources>
                    <DataTemplate x:Key="{x:Static CalendarItem.DayTitleTemplateResourceKey}">
                        <TextBlock Foreground="#4D5E80"  FontWeight="Bold" FontSize="14" HorizontalAlignment="Center" Margin="0,6,0,6" VerticalAlignment="Center">
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource CalendarLangToConverter}" ConverterParameter="DayTitle">
                                    <Binding Path="." UpdateSourceTrigger="PropertyChanged"/>
                                    <Binding Path="DataContext.Lang" RelativeSource="{RelativeSource AncestorType=Window}" UpdateSourceTrigger="PropertyChanged"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </ControlTemplate.Resources>
                <Grid x:Name="PART_Root">
                    <Grid.Resources>
                        <SolidColorBrush x:Key="DisabledColor" Color="#A5FFFFFF"/>
                    </Grid.Resources>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="PART_DisabledVisual" To="1" Storyboard.TargetProperty="Opacity"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1">
                        <Border BorderBrush="#FFFFFFFF" BorderThickness="0" CornerRadius="1">
                            <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" Storyboard.TargetName="path" To="#FF73A9D8" Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"/>
                                                        </Storyboard>
                                                    </VisualState>
                                                    <VisualState x:Name="Disabled">
                                                        <Storyboard>
                                                            <DoubleAnimation Duration="0" Storyboard.TargetName="path" To=".5" Storyboard.TargetProperty="Opacity"/>
                                                        </Storyboard>
                                                    </VisualState>
                                                </VisualStateGroup>
                                            </VisualStateManager.VisualStateGroups>
                                            <Rectangle Fill="#11E5EBF1" Opacity="1" Stretch="Fill"/>
                                            <Grid>
                                                <Path x:Name="path" Data="M288.75,232.25 L288.75,240.625 L283,236.625 z" Fill="#FF333333" HorizontalAlignment="Left" Height="10" Margin="14,-6,0,0" Stretch="Fill" VerticalAlignment="Center" Width="6"/>
                                            </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" Storyboard.TargetName="path" To="#FF73A9D8" Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"/>
                                                        </Storyboard>
                                                    </VisualState>
                                                    <VisualState x:Name="Disabled">
                                                        <Storyboard>
                                                            <DoubleAnimation Duration="0" Storyboard.TargetName="path" To=".5" Storyboard.TargetProperty="Opacity"/>
                                                        </Storyboard>
                                                    </VisualState>
                                                </VisualStateGroup>
                                            </VisualStateManager.VisualStateGroups>
                                            <Rectangle Fill="#11E5EBF1" Opacity="1" Stretch="Fill"/>
                                            <Grid>
                                                <Path x:Name="path" Data="M282.875,231.875 L282.875,240.375 L288.625,236 z" Fill="#FF333333" HorizontalAlignment="Right" Height="10" Margin="0,-6,14,0" Stretch="Fill" VerticalAlignment="Center" Width="6"/>
                                            </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" Storyboard.TargetName="buttonContent" To="#FF73A9D8" Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"/>
                                                        </Storyboard>
                                                    </VisualState>
                                                    <VisualState x:Name="Disabled">
                                                        <Storyboard>
                                                            <DoubleAnimation Duration="0" Storyboard.TargetName="buttonContent" To=".5" Storyboard.TargetProperty="Opacity"/>
                                                        </Storyboard>
                                                    </VisualState>
                                                </VisualStateGroup>
                                            </VisualStateManager.VisualStateGroups>
                                            <ContentPresenter x:Name="buttonContent" ContentTemplate="{TemplateBinding ContentTemplate}" TextElement.FontSize="16" TextElement.FontWeight="Normal" TextElement.Foreground="#4D5E80" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1,0,1,0" VerticalAlignment="Center">
                                                <ContentPresenter.Content>
                                                    <MultiBinding Converter="{StaticResource CalendarLangToConverter}" ConverterParameter="Header">
                                                        <Binding Path="Content" RelativeSource="{RelativeSource TemplatedParent}" UpdateSourceTrigger="PropertyChanged"/>
                                                        <Binding Path="DataContext.Lang" RelativeSource="{RelativeSource AncestorType=Window}" UpdateSourceTrigger="PropertyChanged"/>
                                                    </MultiBinding>
                                                </ContentPresenter.Content>
                                            </ContentPresenter>
                                        </Grid>
                                    </ControlTemplate>
                                </Grid.Resources>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="20"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <Button x:Name="PART_PreviousButton" Grid.Column="0" Focusable="False" HorizontalAlignment="Left" Height="20"  Margin="8,0,8,0" Grid.Row="0" Template="{StaticResource PreviousButtonTemplate}" Width="28"/>
                                <Button x:Name="PART_HeaderButton" Grid.Column="1" Focusable="False" FontWeight="Bold" FontSize="10.5" HorizontalAlignment="Center" Margin="0 0 0 0" Grid.Row="0" Template="{StaticResource HeaderButtonTemplate}" VerticalAlignment="Center"/>
                                <Button x:Name="PART_NextButton" Grid.Column="2" Focusable="False" HorizontalAlignment="Right" Height="20"  Margin="8,0,8,0" Grid.Row="0" Template="{StaticResource NextButtonTemplate}" Width="28"/>
                                <Rectangle x:Name="PA_Line" Grid.Row="1" Grid.ColumnSpan="3"  Height="1" Margin="0 58 0 0" Fill="#EEEEEE" Opacity="0.8" VerticalAlignment="Top"/>
                                <Grid x:Name="PART_MonthView" Grid.ColumnSpan="3" HorizontalAlignment="Center" Margin="6,0,6,6" Grid.Row="1" Visibility="Visible">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="40"/>
                                        <ColumnDefinition Width="40"/>
                                        <ColumnDefinition Width="40"/>
                                        <ColumnDefinition Width="40"/>
                                        <ColumnDefinition Width="40"/>
                                        <ColumnDefinition Width="40"/>
                                        <ColumnDefinition Width="40"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="70"/>
                                        <RowDefinition Height="40"/>
                                        <RowDefinition Height="40"/>
                                        <RowDefinition Height="40"/>
                                        <RowDefinition Height="40"/>
                                        <RowDefinition Height="40"/>
                                        <RowDefinition Height="40"/>
                                    </Grid.RowDefinitions>
                                </Grid>
                                <Grid x:Name="PART_YearView" Grid.ColumnSpan="3" HorizontalAlignment="Center" Margin="6,-3,7,6" Grid.Row="1" VerticalAlignment="Center" Visibility="Hidden">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="70"/>
                                        <ColumnDefinition Width="70"/>
                                        <ColumnDefinition Width="70"/>
                                        <ColumnDefinition Width="70"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="80"/>
                                        <RowDefinition Height="80"/>
                                        <RowDefinition Height="80"/>
                                    </Grid.RowDefinitions>
                                </Grid>
                            </Grid>
                        </Border>
                    </Border>
                    <Rectangle x:Name="PART_DisabledVisual" Fill="{StaticResource DisabledColor}" Opacity="0" RadiusX="2" RadiusY="2" Stroke="{StaticResource DisabledColor}" Stretch="Fill" 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"/>
                        <Setter Property="Visibility" TargetName="PA_Line" Value="Hidden"/>
                    </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"/>
                        <Setter Property="Visibility" TargetName="PA_Line" Value="Hidden"/>
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="CommonCalendarStyle" TargetType="{x:Type Calendar}">
    <Setter Property="CalendarItemStyle" Value="{StaticResource CommonCalendarItemStyle}"/>
    <Setter Property="CalendarDayButtonStyle" Value="{StaticResource CommonCalendarDayButtonStyle}"/>
    <Setter Property="CalendarButtonStyle" Value="{StaticResource CommonCalendarButtonStyle}"/>
    <Setter Property="Foreground" Value="#4D5E80"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Margin" Value="25"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Calendar}">
                <Border x:Name="PART_Root" CornerRadius="10" BorderThickness="1" BorderBrush="#CCC" Background="White" HorizontalAlignment="Center">
                    <Grid>
                        <CalendarItem x:Name="PART_CalendarItem" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Style="{TemplateBinding CalendarItemStyle}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

CalendarLangToConverter 

public class CalendarLangToConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values != null && values[0] != DependencyProperty.UnsetValue && values[1] != null && values[1] != DependencyProperty.UnsetValue)
        {
            CultureInfo cInfo = values[1] as CultureInfo;
            if (parameter.ToString().Equals("Header"))
            {
                DateTime dt;
                if (DateTime.TryParse(values[0].ToString(), out dt))
                {
                    return dt.ToString("Y", cInfo);
                }
            }                
            else if (parameter.ToString().Equals("DayTitle"))
            {
                if (cInfo.IetfLanguageTag.Equals("zh-CN"))
                {
                    switch (values[0].ToString())
                    {
                        case "Mo": return "一";
                        case "Tu": return "二";
                        case "We": return "三";
                        case "Th": return "四";
                        case "Fr": return "五";
                        case "Sa": return "六";
                        case "Su": return "日";
                    }
                }
                if (cInfo.IetfLanguageTag.Equals("en-US"))
                {
                    switch (values[0].ToString())
                    {
                        case "一": return "Mo";
                        case "二": return "Tu";
                        case "三": return "We";
                        case "四": return "Th";
                        case "五": return "Fr";
                        case "六": return "Sa";
                        case "日": return "Su";
                    }
                }
            }
            else if (parameter.ToString().Equals("YearMonth"))
            {
                if (values[0] != null)
                {
                    if (cInfo.IetfLanguageTag.Equals("zh-CN"))
                    {
                        switch (values[0].ToString())
                        {
                            case "Jan": return "1月";
                            case "Feb": return "2月";
                            case "Mar": return "3月";
                            case "Apr": return "4月";
                            case "May": return "5月";
                            case "Jun": return "6月";
                            case "Jul": return "7月";
                            case "Aug": return "8月";
                            case "Sep": return "9月";
                            case "Oct": return "10月";
                            case "Nov": return "11月";
                            case "Dec": return "12月";
                        }
                    }
                    if (cInfo.IetfLanguageTag.Equals("en-US"))
                    {
                        switch (values[0].ToString())
                        {
                            case "1月": return "Jan";
                            case "2月": return "Feb";
                            case "3月": return "Mar";
                            case "4月": return "Apr";
                            case "5月": return "May";
                            case "6月": return "Jun";
                            case "7月": return "Jul";
                            case "8月": return "Aug";
                            case "9月": return "Sep";
                            case "10月": return "Oct";
                            case "11月": return "Nov";
                            case "12月": return "Dec";
                        }
                    }
                }
            }
        }
        return values[0];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

MainWindow.xaml引用

<Window x:Class="WPF_DatePicker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_DatePicker"
        mc:Ignorable="d"
        Title="MainWindow" Height="500" Width="800">
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>

    <Grid>
        <DatePicker Width="110" Height="30" VerticalAlignment="top"     HorizontalAlignment="Right" Margin="0 50 0 0 "
                    CalendarStyle="{StaticResource CommonCalendarStyle}"/>
        
        <Calendar Style="{StaticResource CommonCalendarStyle}"/>

        <StackPanel VerticalAlignment="Center">
            <Button Width="100" Height="40" Content="中文" Command="{Binding ChineseCommand}" HorizontalAlignment="Right"/>
            <Button Width="100" Height="40" Content="英文" Command="{Binding EnglishCommand}" HorizontalAlignment="Right"/>
        </StackPanel>
    </Grid>
</Window>

完整代码如下:

https://download.csdn.net/download/CarryingQiu/90150391

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值