使用样式文件中的setter覆盖控制资源

时间:2017-02-01 15:23:49

标签: c# xaml uwp win-universal-app windows-10-universal

我有代码在XAML中覆盖CalendarDatePicker资源(CalendarDatePickerCalendarGlyphForeground)。

var test = new Test();
test.Things = { "Test" };

现在,我需要重用这段代码,在我的样式文件项目中创建一个通用样式,在我的项目中的其他CalendarDatePicker中使用,如下所示:

<CalendarDatePicker Style="{StaticResource CalendarDatePickerStyle}">
    <CalendarDatePicker.Resources>
        <SolidColorBrush x:Key="CalendarDatePickerCalendarGlyphForeground" Color="{StaticResource PrimaryColor}"/>
    </CalendarDatePicker.Resources>
</CalendarDatePicker>

我应该如何使用Setter做到这一点?

如果我想在所有项目日历中应用通用样式而不必在每个Calendar Style = {StaticResource ...}中输入,我该如何定义这种样式?

1 个答案:

答案 0 :(得分:4)

您可以直接将SolidColorBrush定义添加到App.xaml资源中,它将覆盖默认样式。主要是使用与控件使用相同的Key。

例如,要在每个CalendarDatePicker中使用您自己的颜色覆盖CalendarDatePickerCalendarGlyphForeground,请使App.xaml如下所示:

<Application
x:Class="App1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
RequestedTheme="Light">

<Application.Resources>
    <SolidColorBrush x:Key="CalendarDatePickerCalendarGlyphForeground" Color="Green"/>
</Application.Resources>

此处more info关于UWP中的样式控件。