簡體   English   中英

僅在Style.xaml中,通過事件觸發從ComboBox更改TextBlock前景

[英]Change the TextBlock Foreground from ComboBox over Event fire only in a Style.xaml

我有一個沒有.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>

<Trigger Property="IsMouseOver" Value="True">用於鼠標懸停。

WPF:更改單擊鼠標左鍵時邊框的背景顏色

嘿大家..我可以自己解決! 錯誤是:

Storyboard.TargetName =“ Content” ...當然應該已經引用了“ textBlock”。...希望也能對其他人有所幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM