在 WinRT 中禁用组合框时更改占位符文本颜色 XAML

Change placeholder text color when combobox disabled in WinRT XAML

在我的 Windows 8.1 项目中,我有一个带有自定义样式的组合框,以便我可以更改占位符文本的前景色。

禁用组合框时,此自定义前景色太浅,因此我希望能够更改此占位符文本颜色。

更改 ComboBoxDisabledForegroundThemeBrush 只会影响实际的组合框文本,不会影响占位符文本,而且我看不出有任何方法可以针对不同的视觉状态控制 PlaceholderTextBlock 前景。

占位符文字颜色可以通过视觉状态控制吗?

<Style x:Key="MyCustomStyle" TargetType="ComboBox">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ComboBox">
        <Grid>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualState x:Name="Disabled">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Background">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ComboBoxDisabledBackgroundThemeBrush}"/>
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Background">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ComboBoxDisabledBorderThemeBrush}"/>
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ComboBoxDisabledForegroundThemeBrush}"/>
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="DropDownGlyph">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ComboBoxArrowDisabledForegroundThemeBrush}"/>
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          ...
          <ContentPresenter x:Name="ContentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Grid.Row="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
            <TextBlock x:Name="PlaceholderTextBlock" Foreground="#FFAAAAAA" FontWeight="Normal" Text="{TemplateBinding PlaceholderText}"/>
          </ContentPresenter>

您要找的笔刷在ComboBox的风格下被命名为ComboBoxPlaceholderTextForegroundThemeBrush

 .....
 <ContentPresenter x:Name="ContentPresenter"
               HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
               Margin="{TemplateBinding Padding}"
               Grid.Row="1"
               VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
    <TextBlock x:Name="PlaceholderTextBlock"
               Foreground="{ThemeResource ComboBoxPlaceholderTextForegroundThemeBrush}"
               FontWeight="{ThemeResource ComboBoxPlaceholderTextThemeFontWeight}"
               Text="{TemplateBinding PlaceholderText}" />
 </ContentPresenter>
 ....

你可以像这样单独处理占位符前景

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground"
                               Storyboard.TargetName="PlaceholderTextBlock">
    <DiscreteObjectKeyFrame KeyTime="0"
                            Value="Black" />
</ObjectAnimationUsingKeyFrames>