c#windows phone 8.1更改默认控件颜色

c#windows phone 8.1更改默认控件颜色,c#,visual-studio-2013,windows-phone-8.1,controls,C#,Visual Studio 2013,Windows Phone 8.1,Controls,在我的wp应用程序中,我希望所有控件的颜色都是黑色;对于文本块和文本框,我刚刚在字典中声明了如下样式: <Style x:Key="TextColor" TargetType="TextBlock"> <Setter Property="Foreground" Value="Black"/> </Style> 这样,文本“Day off?”即复选框的内容为黑色,但该框仍为白色。 时间选择器的标头也是如此。我是否必须更改应用程序文本的默认颜色?我该怎

在我的wp应用程序中,我希望所有控件的颜色都是黑色;对于文本块和文本框,我刚刚在字典中声明了如下样式:

<Style x:Key="TextColor" TargetType="TextBlock">
    <Setter Property="Foreground" Value="Black"/>
</Style>
这样,文本“Day off?”即复选框的内容为黑色,但该框仍为白色。


时间选择器的标头也是如此。我是否必须更改应用程序文本的默认颜色?我该怎么做?

您是否尝试覆盖默认笔刷? 如在App.xaml合并词典中添加条目:

<ResourceDictionary.ThemeDictionaries>
  <ResourceDictionary x:Key="Default">
    <SolidColorBrush x:Key="ApplicationForegroundThemeBrush" Color="Magenta" />
  </ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

您可以通过编辑控件的默认样式来编辑任何控件样式。对于
复选框
,它如下所示:

<Style TargetType="CheckBox">
    <Setter Property="Background" Value="{ThemeResource CheckBoxBackgroundThemeBrush}" />
    <Setter Property="BorderBrush" Value="{ThemeResource CheckBoxBorderThemeBrush}" />
    <Setter Property="BorderThickness" Value="{ThemeResource PhoneBorderThickness}" />
    <Setter Property="FontSize" Value="{ThemeResource TextStyleLargeFontSize}" />
    <Setter Property="FontFamily" Value="{ThemeResource PhoneFontFamilyNormal}" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="Padding" Value="{ThemeResource CheckBoxAndRadioButtonTextPaddingThickness}" />
    <Setter Property="MinWidth" Value="{ThemeResource CheckBoxAndRadioButtonMinWidthSize}" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="CheckBox">
          <Grid Background="Transparent">
            <VisualStateManager.VisualStateGroups>
              <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal" />
                <VisualState x:Name="MouseOver" />
                <VisualState x:Name="PointerOver" />
                <VisualState x:Name="Pressed">
                  <Storyboard>
                    <PointerDownThemeAnimation Storyboard.TargetName="Grid" />
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckBackground" Storyboard.TargetProperty="Background">
                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxPressedBackgroundThemeBrush}" />
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Fill">
                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxPressedForegroundThemeBrush}" />
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="Fill">
                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxPressedBackgroundThemeBrush}" />
                    </ObjectAnimationUsingKeyFrames>
                  </Storyboard>
                </VisualState>
                <VisualState x:Name="Disabled">
                  <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckBackground" Storyboard.TargetProperty="BorderBrush">
                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxDisabledBorderThemeBrush}" />
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Fill">
                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxDisabledForegroundThemeBrush}" />
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="Fill">
                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxDisabledBackgroundThemeBrush}" />
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxDisabledForegroundThemeBrush}" />
                    </ObjectAnimationUsingKeyFrames>
                  </Storyboard>
                </VisualState>
                <VisualStateGroup.Transitions>
                  <VisualTransition From="Pressed" To="PointerOver">
                    <Storyboard>
                      <PointerUpThemeAnimation Storyboard.TargetName="Grid" />
                    </Storyboard>
                  </VisualTransition>
                  <VisualTransition From="PointerOver" To="Normal">
                    <Storyboard>
                      <PointerUpThemeAnimation Storyboard.TargetName="Grid" />
                    </Storyboard>
                  </VisualTransition>
                  <VisualTransition From="Pressed" To="Normal">
                    <Storyboard>
                      <PointerUpThemeAnimation Storyboard.TargetName="Grid" />
                    </Storyboard>
                  </VisualTransition>
                </VisualStateGroup.Transitions>
              </VisualStateGroup>
              <VisualStateGroup x:Name="CheckStates">
                <VisualState x:Name="Checked">
                  <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Visibility">
                      <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                    </ObjectAnimationUsingKeyFrames>
                  </Storyboard>
                </VisualState>
                <VisualState x:Name="Unchecked" />
                <VisualState x:Name="Indeterminate">
                  <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="Visibility">
                      <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                    </ObjectAnimationUsingKeyFrames>
                  </Storyboard>
                </VisualState>
              </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Grid x:Name="Grid" Margin="{ThemeResource PhoneTouchTargetLargeOverhang}">
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="25.5" />
                <ColumnDefinition Width="*" />
              </Grid.ColumnDefinitions>
              <Grid Grid.Column="0" VerticalAlignment="Top">
                <Border x:Name="CheckBackground"
                        IsHitTestVisible="False"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Height="25.5"
                        Width="25.5" />
                <Rectangle x:Name="NormalRectangle"
                           IsHitTestVisible="False"
                           Width="13"
                           Height="13"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           Fill="{ThemeResource CheckBoxBackgroundThemeBrush}"
                           Visibility="Collapsed" />
                <Path x:Name="CheckGlyph"
                      IsHitTestVisible="False"
                      Visibility="Collapsed"
                      Width="18.5"
                      Height="17"
                      Stretch="Fill"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center"
                      Fill="{ThemeResource CheckBoxForegroundThemeBrush}"
                      Data="M0,123 L39,93 L124,164 L256,18 L295,49 L124,240 z"
                      StrokeLineJoin="Round"
                      StrokeThickness="2.5"
                      FlowDirection="LeftToRight" />
              </Grid>
              <ContentPresenter x:Name="ContentPresenter"
                                Grid.Column="1" Content="{TemplateBinding Content}"
                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                Margin="{TemplateBinding Padding}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                Foreground="{TemplateBinding Foreground}"
                                FontFamily="{ThemeResource PhoneFontFamilyNormal}"
                                FontSize="{ThemeResource TextStyleLargeFontSize}"
                                FontWeight="Normal"
                                AutomationProperties.AccessibilityView="Raw" />
            </Grid>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
<Style TargetType="TimePicker">
    <Setter Property="FontFamily" Value="{ThemeResource PhoneFontFamilyNormal}" />
    <Setter Property="FontSize" Value="{ThemeResource ContentControlFontSize}" />
    <Setter Property="Foreground" Value="{ThemeResource TimePickerForegroundThemeBrush}" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TimePicker">
                <StackPanel Margin="{TemplateBinding Padding}" x:Name="LayoutRoot">
                    <ContentPresenter x:Name="HeaderContentPresenter" Style="{StaticResource HeaderContentPresenterStyle}" Margin="0,0,0,-3"
                        Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" />
                    <Button x:Name="FlyoutButton"
            Foreground="{TemplateBinding Foreground}"
            BorderBrush="{TemplateBinding Foreground}"
            BorderThickness="2.5"
            Padding="6.5,0,0,3"
            IsEnabled="{TemplateBinding IsEnabled}"
            HorizontalAlignment="Stretch"
            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
它们将改变的含义是自描述性的,它们需要有完全相同的名称

然后,您的
App.xaml
可能如下所示:

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

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <!-- Overriden theme resources -->
            <ResourceDictionary x:Key="Light">
                <SolidColorBrush x:Key="CheckBoxBorderThemeBrush" Color="Black" />
            </ResourceDictionary>
            <ResourceDictionary x:Key="Dark">
                <SolidColorBrush x:Key="CheckBoxBorderThemeBrush" Color="White" />
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>

        <ResourceDictionary.MergedDictionaries>
            <!-- Other resources -->
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
因此,
TimePicker
标题颜色的解决方案是创建一个新的
TimePicker
样式,并设置
HeaderContentPresenter
元素的
前景
,或者您可以只更改
TimePicker
标题模板
,而不创建新样式:

<TimePicker Foreground="Black">
    <TimePicker.Header>
        <TextBlock Text="Header Text Example" Foreground="Black">
    </TimePicker.Header>
</TimePicker>


<Style TargetType="TimePicker">
    <Setter Property="FontFamily" Value="{ThemeResource PhoneFontFamilyNormal}" />
    <Setter Property="FontSize" Value="{ThemeResource ContentControlFontSize}" />
    <Setter Property="Foreground" Value="{ThemeResource TimePickerForegroundThemeBrush}" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TimePicker">
                <StackPanel Margin="{TemplateBinding Padding}" x:Name="LayoutRoot">
                    <ContentPresenter x:Name="HeaderContentPresenter" Style="{StaticResource HeaderContentPresenterStyle}" Margin="0,0,0,-3"
                        Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" />
                    <Button x:Name="FlyoutButton"
            Foreground="{TemplateBinding Foreground}"
            BorderBrush="{TemplateBinding Foreground}"
            BorderThickness="2.5"
            Padding="6.5,0,0,3"
            IsEnabled="{TemplateBinding IsEnabled}"
            HorizontalAlignment="Stretch"
            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<TimePicker Foreground="Black">
    <TimePicker.Header>
        <TextBlock Text="Header Text Example" Foreground="Black">
    </TimePicker.Header>
</TimePicker>
<TimePicker Foreground="Black" Header="Header Text Example">
    <TimePicker.HeaderTemplate>
        <TextBlock Text="{Binding}" Foreground="Black">
    </TimePicker.Header>
</TimePicker>