0

I have a little problem that seems kind of simple, but I can't figure out how to solve it. In Visual Studio I made this app with WPF and inside a Grid with Rows and Columns I placed some buttons. Now each time I start my app the first button in the first row glows as if it was clicked on. I think that looks really weird and I'd like my button to look like the other buttons. Is there any possibility to stop that? Thanks in advance, I' appreciate any help!

Cleptus
  • 3,446
  • 4
  • 28
  • 34
MaChaToc
  • 139
  • 1
  • 12
  • Could it be that the first button is focused? When you press Tab, does the glow effect move to a different button? – wkl Jan 26 '17 at 14:42
  • Yes, I think this is it. The button is focused by default when the app is started. But still is there any possibility to set the focus elsewhere? – MaChaToc Jan 26 '17 at 14:44
  • 1
    You may want to try [this](http://stackoverflow.com/a/1343970/4011717) or set `Button.Focusable` to false. Changing the `TabIndex` might solve it, too. All of these methods have different effects so select carefully... – wkl Jan 26 '17 at 14:52
  • I'm sorry if I got it wrong but isn't the first thing you send me the opposite of what I actually want to achieve? Because in this thread it is about WPF not focusing anything at start but in my case it does – MaChaToc Jan 26 '17 at 14:56
  • You got it right, but at the time I wrote it, I only knew that it was not the first button that should be focused :-). But maybe you can replace the `Binding` by `x:null` in the answer I have linked. Anyway, I did not mean to write a full answer. Only wanted to point you in the right direction. – wkl Jan 26 '17 at 14:59
  • What would work if I set `Button.Focusable` to false for every button... If I do it just for one button the next one is focused – MaChaToc Jan 26 '17 at 14:59
  • What exactly do you mean? Where would I need to place `x:null`? – MaChaToc Jan 26 '17 at 15:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134090/discussion-between-wkl-and-machatoc). – wkl Jan 26 '17 at 15:03
  • Ah ok I'll try that though :) – MaChaToc Jan 26 '17 at 15:03

3 Answers3

1

On Window.Loaded event you could try:

Keyboard.ClearFocus();

It does the trick for me.

Note: This method clears only the logical focus and it's not useful in any scenario.

0

If the behaviour you describe is the focused animation, you can set the focus in another control with the Control.Focus() function.

another_control.Focus();

You can check if it is the focused style what bothers you hitting tab to set the focus to the next control.

Cleptus
  • 3,446
  • 4
  • 28
  • 34
0

You need to use Style for your buttons, In the Style set all the properties of your button like Background , Foreground and you also can assign triggers for various events on buttons. This will prevent button from continuous blinking.

Below is a sample of above(By default I'm setting blue color to buttons)

In the XAML

   <Page>
        <Page.Resources>
                <Style TargetType="{x:Type Button}" x:Key="NonFocusButton">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Name="Border" 
                        Background="Blue"
                        BorderBrush="Blue">
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="Button.IsPressed" Value="True">
                                    <Setter TargetName="Border" Property="BorderBrush" Value="Blue" />
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter TargetName="Border" 
                              Property="Background" Value="Blue" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

        <Button x:Name"myButton" Style="{StaticResource NonFocusButton}" /> //Assign style here
        </Page.Resources>
    </Page>
Tk1993
  • 501
  • 8
  • 21