如何在WPF中应用日历样式?

时间:2016-02-07 14:39:18

标签: wpf xaml datepicker calendar

我试图应用日历样式,但它没有应用。

<DatePicker Grid.Column="1" Margin="5" Background="Transparent"
            CalendarStyle="{StaticResource CalenderStyle1}"/>

样式

<Style x:Key="CalenderStyle1" TargetType="{x:Type Calendar}">
    <Setter Property="Foreground" Value="Red"/>
    <Setter Property="Background" Value="White"/>

    <Setter Property="BorderBrush">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFA3AEB9" Offset="0"/>
                <GradientStop Color="#FF8399A9" Offset="0.375"/>
                <GradientStop Color="#FF718597" Offset="0.375"/>
                <GradientStop Color="#FF617584" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Calendar}">
                <StackPanel x:Name="PART_Root" HorizontalAlignment="Center">
                    <CalendarItem x:Name="PART_CalendarItem" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Style="{StaticResource CalendarItemStyle1}"/>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

样式已应用于UI。我的代码中有错误吗?为什么样式不适用?

1 个答案:

答案 0 :(得分:0)

  1. DatePicker标记中删除背景属性。
  2. BorderThickness属性添加到DatePicker代码或Style