简体   繁体   English

UWP - 按钮 VisualState Focused 无法正常工作

[英]UWP - Button VisualState Focused is not working properly

I'm trying to change the font color of a button when the button is clicked.单击按钮时,我正在尝试更改按钮的字体颜色。 I have tried many different things but none have worked.我尝试了很多不同的东西,但没有一个奏效。 This is the latest thing I tried and what I believe should be the answer but it's not working.这是我尝试过的最新方法,我认为应该是答案,但它不起作用。 Can someone help?有人可以帮忙吗? PointerOver isworking fine, but Focused is not doing anything when the button is clicked. PointerOver 工作正常,但单击按钮时 Focused 没有做任何事情。 After click I want the color to remain applied until another button is clicked, then the text in the new button clicked will change color and the previous button clicked will go back to the default color.单击后,我希望颜色保持应用,直到单击另一个按钮,然后单击的新按钮中的文本将更改颜色,单击的上一个按钮将 go 恢复为默认颜色。

I have tried Pressed and focused state both but none is working.我已经尝试过 Pressed 和 Focused state ,但都没有工作。

        <Style x:Key="ButtonMenuItemStyle"
           TargetType="Button">
        <Setter Property="Background"
                Value="Transparent" />
        <Setter Property="Foreground"
                Value="{x:Bind ViewModel.LeftNavMenuPrimaryTextColor}" />
        <Setter Property="BorderBrush"
                Value="{ThemeResource ButtonBorderBrush}" />
        <Setter Property="BorderThickness"
                Value="0" />
        <Setter Property="Padding"
                Value="0" />
        <Setter Property="AllowFocusOnInteraction"
                Value="True" />
        <Setter Property="HorizontalAlignment"
                Value="Stretch" />
        <Setter Property="VerticalContentAlignment"
                Value="Stretch"></Setter>
        <Setter Property="HorizontalContentAlignment"
                Value="Stretch"></Setter>
        <Setter Property="VerticalAlignment"
                Value="Stretch" />
        <Setter Property="Height"
                Value="65"></Setter>
        <Setter Property="FontFamily"
                Value="{ThemeResource ContentControlThemeFontFamily}" />
        <Setter Property="FontWeight"
                Value="Normal" />
        <Setter Property="FontSize"
                Value="{ThemeResource ControlContentThemeFontSize}" />
        <Setter Property="UseSystemFocusVisuals"
                Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <ContentPresenter x:Name="ContentPresenter"
                                      AutomationProperties.AccessibilityView="Raw"
                                      Background="{TemplateBinding Background}"
                                      BorderThickness="{TemplateBinding BorderThickness}"
                                      BorderBrush="{TemplateBinding BorderBrush}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                      Content="{TemplateBinding Content}"
                                      ContentTransitions="{TemplateBinding ContentTransitions}"
                                      HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      Padding="{TemplateBinding Padding}"
                                      VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal">
                                    <Storyboard>
                                        <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                       Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                                    Value="{StaticResource IconHoverColor}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                               Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0"
                                                            Value="{StaticResource IconHoverTextColor}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                       Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0"
                                                                    Value="{StaticResource SignInDisableStateColor}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" 
                                                                       Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                                    Value="{StaticResource IconHoverColor}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="PointerFocused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" 
                                                                       Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                                    Value="{StaticResource IconHoverColor}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </ContentPresenter>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

After click I want the color to remain applied until another button is clicked, then the text in the new button clicked will change color and the previous button clicked will go back to the default color.单击后,我希望颜色保持应用,直到单击另一个按钮,然后单击的新按钮中的文本将更改颜色,单击的上一个按钮将 go 恢复为默认颜色。

The focus state of the button works when the focus state is triggered from keyboard input.当键盘输入触发焦点 state 时,按钮的焦点 state 起作用。 You could set the button's focus state in the code-behind to check if the VisualState works.您可以在代码隐藏中设置按钮的焦点 state 以检查 VisualState 是否有效。

Like this:像这样:

MyButton.Focus(FocusState.Keyboard);

So want you need to do is put this line of code in the click event of the button.所以想要你需要做的就是把这行代码放在按钮的点击事件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM