Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
80 views
in Technique[技术] by (71.8m points)

c# - Why all buttons receive the same state at once in WPF?

I have the following style for buttons:

    <Style TargetType="Button">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="FontFamily" Value="Roboto" />
        <Setter Property="MinHeight" Value="{StaticResource ButtonMinHeight}" />
        <Setter Property="MinWidth" Value="{StaticResource ButtonMinWidth}" />
        <Setter Property="Padding" Value="{StaticResource ButtonPadding}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="bMain" 
                            CornerRadius="{StaticResource ButtonCornerRadius}" 
                            BorderThickness="{StaticResource ButtonBorderThickness}" 
                            BorderBrush="Transparent" 
                            Background="{StaticResource ButtonBackgroundBrush}"
                            TextBlock.Foreground="{StaticResource ButtonForegroundBrush}">
                        <ContentPresenter Margin="{TemplateBinding Padding}"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            RecognizesAccessKey="True" />
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="bMain" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                            <DiscreteColorKeyFrame KeyTime="0" Value="{StaticResource ButtonHoverBackgroundColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="bMain" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                            <DiscreteColorKeyFrame KeyTime="0" Value="{StaticResource ButtonDownBackgroundColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="bMain" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)">
                                            <DiscreteColorKeyFrame KeyTime="0" Value="{StaticResource ButtonDisabledForegroundColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsDefault" Value="True">
                            <Setter TargetName="bMain" Property="BorderBrush" Value="{StaticResource ButtonBorderBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Also, I have two buttons:

<Button Grid.Column="1" Command="{Binding LogCommand}" Content="{x:Static r:Strings.Main_Button_Log}" Margin="0,0,24,0"/>
<Button Grid.Column="2" Command="{Binding CloseCommand}" Content="{x:Static r:Strings.Main_Button_Close}" />

The problem is that they both seem to receive the same state at the same time.

Normal:

Normal state

Hover (on one of them):

enter image description here

If the images are not distinct enough, in the second one both buttons became highlighted instead of only one. The case is the same for pressing.

How can I fix this?

question from:https://stackoverflow.com/questions/65886003/why-all-buttons-receive-the-same-state-at-once-in-wpf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...