0

我有一个没有 .cs 文件的 Style xaml

您可以在下面找到我的代码的一部分。在那里你可以找到TextBlock. 当用户执行操作(悬停、单击等)时,我想在那里更改字体颜色。如您所见,有一个 VisualStateManager 处理Combobox. 是否可以覆盖 Visual 状态管理器中的前景(例如:如果“单击”则为红色,如果“悬停”则为蓝色)?

我花了很多时间为我的问题找到解决方案,但没有解决方案......

任何人都可以帮助我吗?请注意,该解决方案仅在一个实现的 xaml 文件中,该文件包含我的控件中的所有样式。

<Border x:Name="ContentPresenterBorder"
        BorderBrush="Blue">

    <Grid>
        <ToggleButton x:Name="DropDownToggle"
                      HorizontalAlignment="Stretch"
                      Margin="0"
                      Style="{StaticResource comboToggleStyle}"
                      VerticalAlignment="Stretch"
                      BorderThickness="{TemplateBinding BorderThickness}"
                      HorizontalContentAlignment="Right"
                      Background="White"
                      BorderBrush="{x:Null}">
        </ToggleButton>
        <StackPanel Orientation="Horizontal">
            <TextBlock x:Name="TextTextBlock"
                       Text="{TemplateBinding Tag}"
                       FontWeight="Bold"
                       Foreground="White"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Padding="{TemplateBinding Padding}"
                       Effect="{StaticResource MenuDropShadowEffect}"
                       IsHitTestVisible="False">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseEnter">
                                <ei:ChangePropertyAction PropertyName="Foreground">
                                    <ei:ChangePropertyAction.Value>
                                        <SolidColorBrush Color="#FF04A925" />
                                    </ei:ChangePropertyAction.Value>
                                </ei:ChangePropertyAction>
                            </i:EventTrigger>
                            <i:EventTrigger EventName="MouseLeave">
                                <ei:ChangePropertyAction PropertyName="Foreground">
                                    <ei:ChangePropertyAction.Value>
                                        <SolidColorBrush Color="Black"/>
                                    </ei:ChangePropertyAction.Value>
                                </ei:ChangePropertyAction>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
            </TextBlock>


            <Path x:Name="BtnArrow"
                  Stretch="Uniform"
                  Height="4"
                  HorizontalAlignment="Right"
                  Margin="0,0,6,0"
                  Width="8"
                  Fill="White"
                  Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "
                  Effect="{StaticResource MenuDropShadowEffect}"
                  IsHitTestVisible="False">
            </Path>


        </StackPanel>
        <ContentPresenter x:Name="ContentPresenter"
                          Visibility="Collapsed" />
    </Grid>

</Border>

<Rectangle x:Name="DisabledVisualElement"
           Fill="White"
           RadiusX="3"
           RadiusY="3"
           IsHitTestVisible="false"
           Opacity="0" />
<Rectangle x:Name="FocusVisualElement"
           Stroke="{x:Null}"
           StrokeThickness="1"
           RadiusX="2"
           RadiusY="2"
           Margin="1"
           IsHitTestVisible="false"
           Opacity="0"
           Fill="{x:Null}" />
<Border x:Name="ValidationErrorElement"
        Visibility="Collapsed"
        BorderBrush="#FFDB000C"
        BorderThickness="1"
        CornerRadius="1">
    <ToolTipService.ToolTip>
        <ToolTip x:Name="validationTooltip"
                 DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                 Template="{StaticResource ValidationToolTipTemplate}"
                 Placement="Right"
                 PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
            <ToolTip.Triggers>
                <EventTrigger RoutedEvent="Canvas.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip"
                                                           Storyboard.TargetProperty="IsHitTestVisible">
                                <DiscreteObjectKeyFrame KeyTime="0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <System:Boolean>true</System:Boolean>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </ToolTip.Triggers>
        </ToolTip>
    </ToolTipService.ToolTip>
    <Grid Height="12"
          HorizontalAlignment="Right"
          Margin="1,-4,-4,0"
          VerticalAlignment="Top"
          Width="12"
          Background="Transparent">

    </Grid>
</Border>
<Popup x:Name="Popup"
       Margin="0,1,0,0">
    <Border x:Name="PopupBorder"
            Height="Auto"
            HorizontalAlignment="Stretch"
            CornerRadius="3"
            BorderBrush="#FFC1C1C1"
            BorderThickness="0,0,1,1">
        <Border.Background>
            <LinearGradientBrush EndPoint="0.5,1"
                                 StartPoint="0.5,0">
                <GradientStop Color="#FFFFFFFF"
                              Offset="0" />
                <GradientStop Color="#FFFEFEFE"
                              Offset="1" />
            </LinearGradientBrush>
        </Border.Background>
        <ScrollViewer x:Name="ScrollViewer"
                      BorderThickness="0"
                      Padding="1">
            <ItemsPresenter />
        </ScrollViewer>
    </Border>
</Popup>

<ContentControl x:Name="HeaderText" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Center" >
    <ContentPresenter x:Name="contentPresenter"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    Margin="{TemplateBinding Padding}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
        <ContentPresenter.Effect>
            <DropShadowEffect BlurRadius="0"
                                Color="#FF666666"
                                Direction="90"
                                ShadowDepth="1"/>
        </ContentPresenter.Effect>
    </ContentPresenter>
</ContentControl>

<VisualStateManager.VisualStateGroups>

    <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Normal">
            <Storyboard>

            </Storyboard>
        </VisualState>
        <VisualState x:Name="MouseOver" >
            <!--<Storyboard>
                <ColorAnimation 
                    Storyboard.TargetName="HeaderText" 
                    Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" 
                    Duration="0" To="#FF000000"/>
            </Storyboard>-->
            <Storyboard>
                <ColorAnimationUsingKeyFrames BeginTime="00:00:00"
                                            Storyboard.TargetName="Content" 
                                            Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)">
                    <SplineColorKeyFrame KeyTime="00:00:00.4000000" Value="#FFFF0000"/>
                </ColorAnimationUsingKeyFrames>-->

                <!--<ColorAnimation
                    BeginTime="00:00:00"
                    Duration="00:00:00.0010000"
                    From="#FFFFFFFF" To="#FF000000" 
                    Storyboard.TargetName="Content" 
                    Storyboard.TargetProperty="(UIElement.Foreground).Color"                                            
                />-->
                <ColorAnimation
                    BeginTime="00:00:00"
                    Duration="00:00:00.0010000"
                    From="#FFFFFFFF" To="#FF000000" 
                    Storyboard.TargetName="Content" 
                    Storyboard.TargetProperty="(TextBlock.Foreground).Color"                                            
                />
            </Storyboard>
        </VisualState>
        <VisualState x:Name="Disabled">
            <Storyboard>
                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="DisabledVisualElement"
                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                    <SplineDoubleKeyFrame KeyTime="00:00:00"
                                          Value=".55" />
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
    <VisualStateGroup x:Name="FocusStates">
        <VisualState x:Name="Focused">
            <Storyboard>
                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement"
                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                    <SplineDoubleKeyFrame KeyTime="00:00:00"
                                          Value="1" />
                </DoubleAnimationUsingKeyFrames>
                <ColorAnimationUsingKeyFrames BeginTime="00:00:00"
                                            Storyboard.TargetName="HeaderText" 
                                            Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)">
                    <SplineColorKeyFrame KeyTime="00:00:00.4000000" Value="#FFFF0000"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
        <VisualState x:Name="Unfocused" />
        <VisualState x:Name="FocusedDropDown">
            <Storyboard>

                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                               Duration="00:00:00.0010000"
                                               Storyboard.TargetName="TextTextBlock"
                                               Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.Direction)">
                    <EasingDoubleKeyFrame KeyTime="00:00:00"
                                          Value="-90" />
                </DoubleAnimationUsingKeyFrames>
                <ColorAnimationUsingKeyFrames BeginTime="00:00:00"
                                              Duration="00:00:00.0010000"
                                              Storyboard.TargetName="TextTextBlock"
                                              Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.Color)">
                    <EasingColorKeyFrame KeyTime="00:00:00"
                                         Value="#FF333333" />
                </ColorAnimationUsingKeyFrames>
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                               Duration="00:00:00.0010000"
                                               Storyboard.TargetName="TextTextBlock"
                                               Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.ShadowDepth)">
                    <EasingDoubleKeyFrame KeyTime="00:00:00"
                                          Value="1" />
                </DoubleAnimationUsingKeyFrames>
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                               Duration="00:00:00.0010000"
                                               Storyboard.TargetName="TextTextBlock"
                                               Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.BlurRadius)">
                    <EasingDoubleKeyFrame KeyTime="00:00:00"
                                          Value="2" />
                </DoubleAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Duration="00:00:00"
                                               Storyboard.TargetName="PopupBorder"
                                               Storyboard.TargetProperty="(UIElement.Visibility)">
                    <DiscreteObjectKeyFrame KeyTime="00:00:00">
                        <DiscreteObjectKeyFrame.Value>
                            <Visibility>Visible</Visibility>
                        </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                </ObjectAnimationUsingKeyFrames>
                <!--<ColorAnimation
                    BeginTime="00:00:00"
                    Duration="00:00:00.0010000"
                    Storyboard.TargetName="Content" 
                    Storyboard.TargetProperty="(UIElement.Foreground).(SolidColorBrush.Color)"
                    From="White" To="Black"                                                                     
                />-->
                <ColorAnimationUsingKeyFrames BeginTime="00:00:00"
                                            Storyboard.TargetName="HeaderText" 
                                            Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)">
                    <SplineColorKeyFrame KeyTime="00:00:00.4000000" Value="#FFFF0000"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
    <VisualStateGroup x:Name="ValidationStates">
        <VisualState x:Name="Valid" />
        <VisualState x:Name="InvalidUnfocused">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement"
                                               Storyboard.TargetProperty="Visibility">
                    <DiscreteObjectKeyFrame KeyTime="0">
                        <DiscreteObjectKeyFrame.Value>
                            <Visibility>Visible</Visibility>
                        </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
        <VisualState x:Name="InvalidFocused">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement"
                                               Storyboard.TargetProperty="Visibility">
                    <DiscreteObjectKeyFrame KeyTime="0">
                        <DiscreteObjectKeyFrame.Value>
                            <Visibility>Visible</Visibility>
                        </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip"
                                               Storyboard.TargetProperty="IsOpen">
                    <DiscreteObjectKeyFrame KeyTime="0">
                        <DiscreteObjectKeyFrame.Value>
                            <System:Boolean>True</System:Boolean>
                        </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>
4

2 回答 2

0

嘿伙计们..我可以自己解决!错误是:

Storyboard.TargetName="Content"... 当然应该指的是“textBlock”...希望对其他人也有帮助...

于 2013-01-24T08:52:48.653 回答
0

<Trigger Property="IsMouseOver" Value="True">用于鼠标悬停。

WPF:更改鼠标左键下边框的背景颜色以进行鼠标单击

于 2013-01-23T16:47:28.307 回答