Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Enter key invokes button click

Enter key invokes button click

Scheduled Pinned Locked Moved WPF
wpfcsharpcssdatabasedebugging
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrooverFromHolland
    wrote on last edited by
    #1

    Hi all, This is my very first WPf-project, so please have some consideration and don’t advise me on MVVM . Everything I in my project is working fine except one thing: Whenever I hit the enter key on my keyboard a button gets the click event. This button is not default button and the only event registered with this button is the click-event. This is driving me insane.:confused: If I put a breakpoint in the click event handler the routedEventsArgs e is button base click. Here is the code related to this problem:

            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="IsEnabled" Value="True" />
            <Setter Property="Width" Value="Auto" />
            <Setter Property="Height" Value="Auto" />
            <Setter Property="Margin" Value="14,5,0,5" />            
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Offset="0" Color="Black" />
                        <GradientStop Offset="1" Color="#FFC41B1B" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter x:Name="MyContentPresenter"
                                              HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              Content="{TemplateBinding Content}" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" Value="#7d7f74" />
                    <Setter Property="Background" Value="#31302c" />
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="true">
                    <Setter Property="Foreground" Value="Goldenrod" />
                </Trigger>
                <Trigger Property="IsMouseOver" Va</x-turndown>
    
    M 1 Reply Last reply
    0
    • G GrooverFromHolland

      Hi all, This is my very first WPf-project, so please have some consideration and don’t advise me on MVVM . Everything I in my project is working fine except one thing: Whenever I hit the enter key on my keyboard a button gets the click event. This button is not default button and the only event registered with this button is the click-event. This is driving me insane.:confused: If I put a breakpoint in the click event handler the routedEventsArgs e is button base click. Here is the code related to this problem:

              <Setter Property="OverridesDefaultStyle" Value="True" />
              <Setter Property="SnapsToDevicePixels" Value="True" />
              <Setter Property="Foreground" Value="White" />
              <Setter Property="IsEnabled" Value="True" />
              <Setter Property="Width" Value="Auto" />
              <Setter Property="Height" Value="Auto" />
              <Setter Property="Margin" Value="14,5,0,5" />            
              <Setter Property="Background">
                  <Setter.Value>
                      <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                          <GradientStop Offset="0" Color="Black" />
                          <GradientStop Offset="1" Color="#FFC41B1B" />
                      </LinearGradientBrush>
                  </Setter.Value>
              </Setter>
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="{x:Type Button}">
                          <Grid Background="{TemplateBinding Background}">
                              <ContentPresenter x:Name="MyContentPresenter"
                                                HorizontalAlignment="Center"
                                                VerticalAlignment="Center"
                                                Content="{TemplateBinding Content}" />
                          </Grid>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
              <Style.Triggers>
                  <Trigger Property="IsEnabled" Value="false">
                      <Setter Property="Foreground" Value="#7d7f74" />
                      <Setter Property="Background" Value="#31302c" />
                  </Trigger>
                  <Trigger Property="IsKeyboardFocused" Value="true">
                      <Setter Property="Foreground" Value="Goldenrod" />
                  </Trigger>
                  <Trigger Property="IsMouseOver" Va</x-turndown>
      
      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      If your button has the focus then enter will invoke the click event. If all other controls are read only then your button may automatically get the focus.

      Never underestimate the power of human stupidity RAH

      G 1 Reply Last reply
      0
      • M Mycroft Holmes

        If your button has the focus then enter will invoke the click event. If all other controls are read only then your button may automatically get the focus.

        Never underestimate the power of human stupidity RAH

        G Offline
        G Offline
        GrooverFromHolland
        wrote on last edited by
        #3

        Thank You for Your reply, now it is obvious that the button automatically gets focus, but I could not see difference in appearance of the button. I'll fix that. How can I prevent any control to get keyboard focus other than with the TAB-key or mouse-click? It is a disaster when my application gets a reset when the ENTER-key is accidentally hit.

        0200 A9 23 0202 8D 01 80 0205 00

        M 1 Reply Last reply
        0
        • G GrooverFromHolland

          Thank You for Your reply, now it is obvious that the button automatically gets focus, but I could not see difference in appearance of the button. I'll fix that. How can I prevent any control to get keyboard focus other than with the TAB-key or mouse-click? It is a disaster when my application gets a reset when the ENTER-key is accidentally hit.

          0200 A9 23 0202 8D 01 80 0205 00

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Take a look into the FocusManager, this is my primary grid declaration.

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups