将样式应用于 wpf 自定义日历

Apply styles to wpf Custom calendars

我创建了一个日历,如下所示

<Grid>
        <Rectangle Margin="2" Height="25" Name="borderRectangle" VerticalAlignment="Top" Fill="#FFEAEEF9" />
        <Button Name="titleButton" Style="{StaticResource ButtonStyle}" Margin="30,1,30,0"  FlowDirection="RightToLeft" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0" Focusable="True" Click="titleButton_Click" Height="25" VerticalAlignment="Top" FontWeight="Bold" IsTabStop="True" TabIndex="0">خرداد 1397</Button>
        <Button Name="previousButton" Style="{StaticResource ButtonStyle}" Height="25" HorizontalAlignment="Right" Margin="0,2,12,0"  VerticalAlignment="Top" Width="23"  Background="Transparent" BorderThickness="0" Padding="0"  FontFamily="Arial" FontSize="14" BorderBrush="Transparent" Click="previousButton_Click" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" IsTabStop="True" TabIndex="0">►</Button>
        <Button Name="nextButton" Style="{StaticResource ButtonStyle}" Height="25" HorizontalAlignment="Left" Margin="12,2,0,0" VerticalAlignment="Top" Width="23"  Background="Transparent" BorderThickness="0" Padding="0" FontFamily="Arial" FontSize="14" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" BorderBrush="Transparent"  Click="nextButton_Click" IsTabStop="True" TabIndex="0">◄</Button>
        <UniformGrid Margin="3,26,3,2" Name="monthUniformGrid" Rows="7" Columns="7"  FlowDirection="RightToLeft" />
        <UniformGrid Margin="3,26,3,2" Name="yearUniformGrid"  Columns="3" Rows="4" FlowDirection="RightToLeft" />
        <UniformGrid Margin="3,26,3,2" Name="decadeUniformGrid"  Columns="3" Rows="4" FlowDirection="RightToLeft" />
    </Grid>

结果是这样的

现在我有了另一个具有漂亮样式的日历控件我可以访问样式,但是当我应用它时出现错误

Style="{StaticResource CalendarBaseStyle}"

calendar target type does not match type of element... and this is my style

 <Style x:Key="CalendarBaseStyle" TargetType="{x:Type Calendar}">
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Background" Value="White" />
        <Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}" />
...

如您所见,我的日历是用Uniform Grid制作的,那么如何应用日历样式呢?

将样式目标类型更改为您的控件类型,例如:

xmlns:myControls="clr-namespace:MyNamespace.Controls"
....
<Style x:Key="CalendarBaseStyle" TargetType="{x:Type myControls:MyCalendarControl}">
....