0

我正在使用 WPF 模板的自定义模板。但是,在大多数情况下,当我将鼠标悬停在按钮上时,它会更改同一屏幕上所有按钮的文本颜色,这是无意的。

<Style  TargetType="{x:Type Button}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="OverridesDefaultStyle"
      Value="true" />
    <Setter Property="FocusVisualStyle"
      Value="{DynamicResource ButtonFocusVisual}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border TextBlock.Foreground="{DynamicResource AlternateFontColor}"
                        TextBlock.FontFamily="Segoe UI"
                        TextBlock.FontWeight="Bold"                            
                        x:Name="Border"
                        CornerRadius="0"
                        BorderThickness="2" >
                    <Border.BorderBrush>
                        <SolidColorBrush Color="{DynamicResource ControlMouseOverColor}" />
                    </Border.BorderBrush>
                    <Border.Background>
                        <SolidColorBrush Color="{DynamicResource ColorBackgroundColor}" />
                    </Border.Background>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0:0:0.0" />
                                <VisualTransition GeneratedDuration="0"
                              To="Pressed" />
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
                  (SolidColorBrush.Color)"
                                            Storyboard.TargetName="Border">
                                        <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource ControlMouseOverColor}" />
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
                                                                 Storyboard.TargetName="Border">
                                        <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlBorderMouseOverColor}" />
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Border">
                                        <EasingColorKeyFrame KeyTime="0" Value="White" />
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
                  (SolidColorBrush.Color)"
                                            Storyboard.TargetName="Border">
                                        <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource ControlPressedColor}" />
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
                                                                 Storyboard.TargetName="Border">
                                        <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlBorderMouseOverColor}" />
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentPresenter Margin="2"
                                      HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                      VerticalAlignment="Center"
                                      RecognizesAccessKey="True"
                                      TextBlock.TextAlignment="Center"

                                      >                                        

                    </ContentPresenter>

                </Border>
                <!-- Triggers -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
4

1 回答 1

1

将样式声明更新为

 <Style  x:Key="buttonStyle"  TargetType="{x:Type Button}">

并将样式分配给按钮,如果它们必须被设置为

 <Button Style="{DynamicResource buttonStyle}">H2</Button>

声明 x:Key 后其他按钮不会继承样式

于 2012-10-06T17:03:16.153 回答