可编辑的Combobox文本颜色无法正常工作

时间:2015-07-01 09:22:55

标签: c# wpf vb.net xaml combobox

我为我的应用程序创建了一个Combobox ControlTemplate。 作为基础,我使用了MS-Page中的默认模板 这个模板只有一个问题,如果Combobox接受输入,它不会改变文本颜色。为了解决这个问题,我添加了

     <ColorAnimationUsingKeyFrames Storyboard.TargetName="PART_EditableTextBox"
                                            Storyboard.TargetProperty="(TextElement.Foreground).
                  (SolidColorBrush.Color)">
                            <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource Font_Color}" />
                        </ColorAnimationUsingKeyFrames>

他们为VisualState设置样式&#34;可编辑&#34;它自己的Combobox。现在它有效。但只要我不禁用控件并再次启用它。如果我这样做,颜色将返回其原始默认颜色,该颜色为黑色,几乎在我的界面上不可见^^

所以:我需要更改什么才能使文本返回到我在停用之前给出的颜色?

以下是我对Combobox和子控件的完整xaml代码:

    <ControlTemplate x:Key="DefaultComboboxTemplate" TargetType="{x:Type ComboBox}">
    <Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal" />
                <VisualState x:Name="MouseOver" />
                <VisualState x:Name="Disabled">
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="PART_EditableTextBox"
                                            Storyboard.TargetProperty="(TextElement.Foreground).
                  (SolidColorBrush.Color)">
                            <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource Font_Color}" />
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="EditStates">
                <VisualState x:Name="Editable">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                             Storyboard.TargetName="PART_EditableTextBox">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                        Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames
                  Storyboard.TargetProperty="(UIElement.Visibility)"
                                             Storyboard.TargetName="ContentSite">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                        Value="{x:Static Visibility.Hidden}" />
                        </ObjectAnimationUsingKeyFrames>

                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="PART_EditableTextBox"
                                            Storyboard.TargetProperty="(TextElement.Foreground).
                  (SolidColorBrush.Color)">
                            <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource Font_Color}" />
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Uneditable" />
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <ToggleButton x:Name="ToggleButton"
                    Template="{DynamicResource ComboBoxToggleButtonTemplate}"
                    Grid.Column="2"
                    Focusable="false"
                    ClickMode="Press"
                    IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, 
          RelativeSource={RelativeSource TemplatedParent}}"/>
        <ContentPresenter x:Name="ContentSite"
                        IsHitTestVisible="False"
                        Content="{TemplateBinding SelectionBoxItem}"
                        ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                        Margin="3,3,23,3"
                        VerticalAlignment="Stretch"
                        HorizontalAlignment="Right">
        </ContentPresenter>
        <TextBox x:Name="PART_EditableTextBox"
               Style="{x:Null}"
               Template="{DynamicResource ComboBoxTextBoxTemplate}"
               HorizontalAlignment="Right"
               VerticalAlignment="Bottom"
               Margin="3,3,23,3"
               Focusable="True"
               Background="{StaticResource BG_Brush}"
               Visibility="Hidden"
               IsReadOnly="{TemplateBinding IsReadOnly}" />
        <Popup x:Name="Popup"
             Placement="Bottom"
             IsOpen="{TemplateBinding IsDropDownOpen}"
             AllowsTransparency="True"
             Focusable="False"
             PopupAnimation="Slide"                                  >

            <Grid x:Name="DropDown"
              SnapsToDevicePixels="True"
              MinWidth="{TemplateBinding ActualWidth}"
              MaxHeight="{TemplateBinding MaxDropDownHeight}">
                <Border x:Name="DropDownBorder"
                  BorderThickness="1" Margin="0,0,0,20" OpacityMask="{DynamicResource PopUp_Opacity}" Background="{DynamicResource PopUp_BG}">
                    <Border.BorderBrush>
                        <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
                    </Border.BorderBrush>

                </Border>
                <ScrollViewer Margin="4,6,4,20"
                        SnapsToDevicePixels="True">
                    <StackPanel IsItemsHost="True"
                        KeyboardNavigation.DirectionalNavigation="Contained" HorizontalAlignment="Right"/>
                </ScrollViewer>
            </Grid>
        </Popup>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="HasItems"
               Value="false">
            <Setter TargetName="DropDownBorder"
                Property="MinHeight"
                Value="95" />
        </Trigger>
        <Trigger Property="IsGrouping"
               Value="true">
            <Setter Property="ScrollViewer.CanContentScroll"
                Value="false" />
        </Trigger>
        <Trigger SourceName="Popup"
               Property="AllowsTransparency"
               Value="true">
            <Setter TargetName="DropDownBorder"
                Property="CornerRadius"
                Value="4" />
            <Setter TargetName="DropDownBorder"
                Property="Margin"
                Value="0,2,0,0" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Combobox的ToggleButton

<ControlTemplate x:Key="ComboBoxToggleButtonTemplate"
             TargetType="{x:Type ToggleButton}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="20" />
        </Grid.ColumnDefinitions>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal" />
                <VisualState x:Name="MouseOver">
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
            (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                      Storyboard.TargetName="Border">
                            <EasingColorKeyFrame KeyTime="0"
                               Value="{StaticResource ControlMouseOverColor}" />
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Pressed" />
                <VisualState x:Name="Disabled">
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
            (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                      Storyboard.TargetName="Border">
                            <EasingColorKeyFrame KeyTime="0"
                               Value="{StaticResource DisabledControlDarkColor}" />
                        </ColorAnimationUsingKeyFrames>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).
            (SolidColorBrush.Color)"
                                      Storyboard.TargetName="Arrow">
                            <EasingColorKeyFrame KeyTime="0"
                               Value="{StaticResource DisabledForegroundColor}" />
                        </ColorAnimationUsingKeyFrames>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).
            (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                      Storyboard.TargetName="Border">
                            <EasingColorKeyFrame KeyTime="0"
                               Value="{StaticResource DisabledBorderDarkColor}" />
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="CheckStates">
                <VisualState x:Name="Checked">
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
            (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                      Storyboard.TargetName="Border">
                            <EasingColorKeyFrame KeyTime="0"
                               Value="{StaticResource ControlPressedColor}" />
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Unchecked" />
                <VisualState x:Name="Indeterminate" />
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Border x:Name="Border"
        Grid.ColumnSpan="2"
        CornerRadius="2"
        BorderThickness="1">
            <Border.BorderBrush>
                <LinearGradientBrush EndPoint="0,1"
                         StartPoint="0,0">
                    <GradientStop Color="{DynamicResource BG_Color}"
                    Offset="0" />
                    <GradientStop Color="{DynamicResource BG_Color_2}"
                    Offset="1" />
                </LinearGradientBrush>
            </Border.BorderBrush>
            <Border.Background>

                <LinearGradientBrush StartPoint="0,0"
                         EndPoint="0,1">
                    <LinearGradientBrush.GradientStops>
                        <GradientStopCollection>
                            <GradientStop Color="{DynamicResource BG_Color}" />
                            <GradientStop Color="{DynamicResource ControlMediumColor}"
                        Offset="1.0" />
                        </GradientStopCollection>
                    </LinearGradientBrush.GradientStops>
                </LinearGradientBrush>

            </Border.Background>
        </Border>
        <Border Grid.Column="0"
        CornerRadius="2,0,0,2"
        Margin="1" >
            <Border.Background>
                <SolidColorBrush Color="{DynamicResource ControlLightColor}"/>
            </Border.Background>
        </Border>
        <Path x:Name="Arrow"
      Grid.Column="1"
      HorizontalAlignment="Center"
      VerticalAlignment="Center"
      Data="M 0 0 L 4 4 L 8 0 Z" >
            <Path.Fill>
                <SolidColorBrush Color="{StaticResource Font_Color}"/>
            </Path.Fill>
        </Path>
    </Grid>
</ControlTemplate>

Combobox的文本框:

<ControlTemplate x:Key="ComboBoxTextBoxTemplate"
             TargetType="{x:Type TextBox}" >
    <Border x:Name="PART_ContentHost"
      Focusable="False"
      Background="{TemplateBinding Background}" 
        />
</ControlTemplate>

ComboboxItem的默认版本

<ControlTemplate x:Key="DefaultComboboxItemTemplate" TargetType="{x:Type ComboBoxItem}">
    <Border x:Name="Border"
            Padding="2"
            SnapsToDevicePixels="true"
            Background="{DynamicResource BG_Brush}" HorizontalAlignment="Right">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="SelectionStates">
                <VisualState x:Name="Unselected" />
                <VisualState x:Name="Selected">
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                            Storyboard.TargetProperty="(Panel.Background).
                (SolidColorBrush.Color)">
                            <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource SelectedBackgroundColor}" />
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="SelectedUnfocused">
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                            Storyboard.TargetProperty="(Panel.Background).
                (SolidColorBrush.Color)">
                            <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource SelectedUnfocusedColor}" />
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <ContentPresenter />
    </Border>
</ControlTemplate>

1 个答案:

答案 0 :(得分:0)

找到它,
添加

        <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Foreground" Value="{StaticResource Font_Brush}"/>
        </Trigger>
        <Trigger Property="IsEditable"
           Value="true">
            <Setter Property="IsTabStop" Value="false"/>
            <Setter TargetName="PART_EditableTextBox" Property="Visibility"    Value="Visible"/>
            <Setter TargetName="PART_EditableTextBox" Property="Foreground" Value="{StaticResource Font_Brush}"/>
            <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
        </Trigger>

触发器元素下的DefaultCombobox的ControlTemplate为我修复了问题