【问题标题】:CalendarStyle of DatePicker different than my custom CalendarStyleDatePicker 的 CalendarStyle 与我的自定义 CalendarStyle 不同
【发布时间】:2015-05-26 23:16:41
【问题描述】:

我有一个自定义日历样式和一个自定义 DatePicker 样式。

MyCustomSkin.xaml 中的代码(来自我的库项目)

<Style TargetType="{x:Type Calendar}">
    <Setter Property="Background" Value="Red"/>
</Style>
<Style TargetType="{x:Type DatePicker}">
    <Setter Property="Background" Value="Green"/>
</Style>

它们都在我的应用程序中引用的 MyCustomSkin.xaml ResourceDictionary(生成 MyCustomSkin.dll 的单独解决方案)中。

我的 App.xaml 中的代码(来自我的应用程序)

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MyCustomSkin;component/MyCustomSkin.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xaml 中的代码(来自我的应用程序)

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
    <Grid>
        <DatePicker HorizontalAlignment="Right" />
        <Calendar HorizontalAlignment="Left"/>
    </Grid>
</Window>

当我在我的应用程序中创建日历时(没有指定任何样式),背景是红色的。 当我在我的应用程序中创建 DatePicker 时(未指定任何样式),背景是绿色,但其日历的背景是系统日历的背景(=> 不是红色)。

为什么?我想我正在用我的覆盖所有系统日历。为什么在 DatePicker 的 Calendar 上应用了 Calendar 的自定义样式而不是?

谢谢!

【问题讨论】:

    标签: c# wpf datepicker calendar styles


    【解决方案1】:

    您的 MyCustomSkin.xaml 应该是:

    <Style TargetType="{x:Type Calendar}" >
                    <Setter Property="Background" Value="Red" />
                </Style>
    
                <Style TargetType="{x:Type DatePicker}" >
                    <Setter Property="Background" Value="Green" />
                    <Setter Property="CalendarStyle">
                        <Setter.Value>
                            <Style TargetType="{x:Type Calendar}" BasedOn="{StaticResource {x:Type Calendar}}"/>
    
                        </Setter.Value>
                    </Setter>
                </Style>         
    

    DatePicker 有一个默认的 CalendarStyle,它将应用于组件初始化。

    【讨论】:

    • 我在一个单独的解决方案(.dll)中有我的样式。我不想提供 x:key,因为我希望我的所有日​​历都具有我定义的样式。我不想在我的应用程序中指定日历的样式。我希望我的应用程序自动使用定义的样式。它正在工作,因为我只需在我的代码中添加 就可以获得带有红色背景的日历。我刚刚用更多细节编辑了我的问题。
    • 使用您的(新)解决方案,我将需要复制我的代码(将我的日历样式用于“普通”日历,并将本地日历样式用于 DatePicker)。这是我不想做的事情,但我不知道这是否可能。
    • 除了“在我的 DatePicker 中复制本地日历样式”或“给我的日历样式添加 x:key”之外,我是否别无选择?
    • 如果您想避免代码重复,请将 DatePicker CalendarStyle 基础设置为应用程序日历样式。用 basedOn 样式编辑了我的答案
    • 我试了一下,还是系统默认样式:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多