仅在 Style.xaml 中将 ComboBox 从 ComboBox 更改为事件触发

本文关键字:ComboBox 事件 Style xaml 中将 仅在 | 更新日期: 2023-09-27 17:55:37

我有一个没有.cs文件的样式xaml

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

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

有人可以帮助我吗?请注意,该解决方案仅在实现的 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>

仅在 Style.xaml 中将 ComboBox 从 ComboBox 更改为事件触发

鼠标

悬停的<Trigger Property="IsMouseOver" Value="True">

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

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

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