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

时间:2016-02-09 13:26:09

标签: c# xaml combobox windows-runtime 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>

1 个答案:

答案 0 :(得分:3)

您正在寻找的画笔在ComboBoxPlaceholderTextForegroundThemeBrush的风格下被命名为ComboBox

 .....
 <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>