Enter key invokes button click
-
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>
-
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>
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
-
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
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
-
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
Take a look into the FocusManager, this is my primary grid declaration.
Never underestimate the power of human stupidity RAH