wpf更改日历日标题前景颜色。

时间:2022-11-21 17:42:38

I am trying to change calendar day title foreground color. I am using standard .net 4.0 Datepicker. The calendar is embedded in the Datepicker.

我正在尝试更改日历天标题前景色。我使用的是标准的。net 4.0 Datepicker。日历嵌入在Datepicker中。

I have the following code in the resource file. but it does not work.

我在资源文件中有以下代码。但它不起作用。

<Style  TargetType="{x:Type CalendarItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CalendarItem}">
                <ControlTemplate.Resources>
                    <DataTemplate x:Key="DayTitleTemplate">
                        <TextBlock
                                            FontWeight="Bold" 
                                            FontFamily="Verdana" 
                                            FontSize="9.5" 
                                            Foreground="Red" 
                                            HorizontalAlignment="Center"
                                            Text="{Binding}"
                                            Margin="0,6,0,6"
                                            VerticalAlignment="Center"/>
                    </DataTemplate>
                </ControlTemplate.Resources>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Can anyone help see what I am missing here? Thanks,

有人能帮我看看我在这里错过了什么吗?谢谢,

2 个解决方案

#1


0  

Take a look at this MSDN Article, it is about customizing the WPF Calendar controls and has this excerpt on the DatePicker Control.

看看这篇MSDN文章,它是关于自定义WPF日历控件的,并且在DatePicker控件上有这段摘录。

From Link (emphasis mine):

我从链接(重点):

But all the styles and templates you can apply to the standalone Calendar control can also be applied to the Calendar control invoked from the dropdown in DatePicker. The DatePicker control has a property named CalendarStyle of type Style, and the Style object you set to this property can contain setters for any property defined by Calendar, including the CalendarItemStyle, CalendarButtonStyle, and CalendarDayButtonStyle properties.

但是,您可以应用于独立日历控件的所有样式和模板也可以应用于从DatePicker的下拉菜单中调用的日历控件。DatePicker控件有一个名为CalendarStyle的类型样式属性,您设置的样式对象可以包含日历定义的任何属性的setter,包括Calendar aritemstyle、Calendar arbuttonstyle和CalendarDayButtonStyle属性。

See this link for the DatePicker Template.

查看DatePicker模板的链接。

#2


0  

Unfortunately the foreground property is hard-coded in the default control template. The way to change it is to copy-and-modify the template.

不幸的是,前台属性是在默认控件模板中硬编码的。改变它的方法是复制和修改模板。

<Style x:Key="CalendarItemStyle" TargetType="{x:Type CalendarItem}">
    <Setter Property="Margin" Value="0,3,0,3" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CalendarItem}">
                    <ControlTemplate.Resources>
                        <DataTemplate x:Key="{x:Static CalendarItem.DayTitleTemplateResourceKey}">
                            <TextBlock 
                                <!-- Day header color here -->
                                Foreground="Red"
                                FontWeight="Bold"
                                FontSize="9.5"
                                FontFamily="Verdana"
                                Margin="0,6,0,6"
                                Text="{Binding}"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center" />
                        </DataTemplate>

You also might want to use Foreground="{TemplateBinding}", so that way you can modify the color by setting the Foreground property on individual controls.

您可能还想使用前景=“{TemplateBinding}”,这样您就可以通过在各个控件上设置前景属性来修改颜色。

#1


0  

Take a look at this MSDN Article, it is about customizing the WPF Calendar controls and has this excerpt on the DatePicker Control.

看看这篇MSDN文章,它是关于自定义WPF日历控件的,并且在DatePicker控件上有这段摘录。

From Link (emphasis mine):

我从链接(重点):

But all the styles and templates you can apply to the standalone Calendar control can also be applied to the Calendar control invoked from the dropdown in DatePicker. The DatePicker control has a property named CalendarStyle of type Style, and the Style object you set to this property can contain setters for any property defined by Calendar, including the CalendarItemStyle, CalendarButtonStyle, and CalendarDayButtonStyle properties.

但是,您可以应用于独立日历控件的所有样式和模板也可以应用于从DatePicker的下拉菜单中调用的日历控件。DatePicker控件有一个名为CalendarStyle的类型样式属性,您设置的样式对象可以包含日历定义的任何属性的setter,包括Calendar aritemstyle、Calendar arbuttonstyle和CalendarDayButtonStyle属性。

See this link for the DatePicker Template.

查看DatePicker模板的链接。

#2


0  

Unfortunately the foreground property is hard-coded in the default control template. The way to change it is to copy-and-modify the template.

不幸的是,前台属性是在默认控件模板中硬编码的。改变它的方法是复制和修改模板。

<Style x:Key="CalendarItemStyle" TargetType="{x:Type CalendarItem}">
    <Setter Property="Margin" Value="0,3,0,3" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CalendarItem}">
                    <ControlTemplate.Resources>
                        <DataTemplate x:Key="{x:Static CalendarItem.DayTitleTemplateResourceKey}">
                            <TextBlock 
                                <!-- Day header color here -->
                                Foreground="Red"
                                FontWeight="Bold"
                                FontSize="9.5"
                                FontFamily="Verdana"
                                Margin="0,6,0,6"
                                Text="{Binding}"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center" />
                        </DataTemplate>

You also might want to use Foreground="{TemplateBinding}", so that way you can modify the color by setting the Foreground property on individual controls.

您可能还想使用前景=“{TemplateBinding}”,这样您就可以通过在各个控件上设置前景属性来修改颜色。