Home > Mobile >  WPF pressed state for enter key just in styles
WPF pressed state for enter key just in styles

Time:11-18

I have a button with custom style and a trigger for the pressed state:

<ControlTemplate.Triggers>
    <Trigger Property="IsPressed" Value="True">
    <Setter Property="Background" TargetName="BorderButton" Value="#0061AE" />
</ControlTemplate.Triggers>

This pressed state works if I press the space key on an focused button. But on with the enter key the pressed state is not seen and the action of the button is fired without any effects. If I hold the space key on an focused button than I can see the effect. On enter not. My question now: Is it possible to add the same functionality that I have for the space key also for the enter key without code behind and just change something in the styles?

Here is my Button:

 <Button Style="{DynamicResource BrandedButtonStyle}" Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="2" 
                    Width="316"
                    HorizontalAlignment="Left" 
                    Margin="40,0,40,0"
                    Content="{i18n:Translate Wizard_Btn_Next}" 
                    Command="{Binding Path=NextCommand}" 
                    AutomationProperties.AutomationId="ButtonNext"/>

and here is the style:

<Style x:Key="BrandedButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="{DynamicResource Secondary}" />
        <Setter Property="Foreground" Value="{DynamicResource OnSecondary}" />
        <Setter Property="FontSize" Value="14" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="MinWidth" Value="132" />
        <Setter Property="MinHeight" Value="32" />
        <Setter Property="Padding" Value="12,8" />
        <Setter Property="FocusVisualStyle" Value="{DynamicResource ButtonFocusVisual}" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}" x:Name="BorderButton" BorderThickness="0">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,0" />
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" TargetName="BorderButton" Value="{DynamicResource SecondaryPress}" />
                            <Setter Property="Foreground" Value="{DynamicResource OnSecondary}" />
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" Value="{DynamicResource OnBackground10}" />
                            <Setter Property="Foreground" Value="{DynamicResource OnBackground30}" />
                        </Trigger>

                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="{DynamicResource SecondaryHover}" />
                            <Setter Property="Foreground" Value="{DynamicResource OnSecondary}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

CodePudding user response:

Your problem is due to the fact that the state of the IsPressed property does not change on the Enter key.
Do a simple test:

    <Button x:Name="pressedButton" Content="Pressed"/>
        public MainWindow()
        {
            InitializeComponent();

            DependencyPropertyDescriptor.FromProperty(ButtonBase.IsPressedProperty, typeof(ButtonBase))
                .AddValueChanged(pressedButton,
                                 (s, e) =>
                                 {
                                     ButtonBase button = (ButtonBase)s;
                                     Debug.WriteLine($"IsPressed: {button.IsPressed}");
                                 });

To solve this problem, you will have to implement an Attached Property that changes its state when Enter is pressed when the button is focused.

  • Related