Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to hide the gridlines when pressing a button in WPF C#?

I have been working on some projects and I can’t seem to find a way to hide gridlines that appear when you press a button like in the image below

enter image description here

is there a way that will hide the gridlines? I don’t want the user when press a button in my application to see the gridlines.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is an example of my button xaml code:

<Button Style="{DynamicResource HeaderButtonStyle}" Grid.Column="0" Background="Transparent" Width="Auto" BorderBrush="Transparent" Click="MinButton_Click">
        <StackPanel Orientation="Horizontal">
           <Image RenderOptions.BitmapScalingMode="HighQuality" Source="/Resources/icons8-minimize-window-100.png"/>
        </StackPanel>
</Button>

>Solution :

You can set the FocusVisualStyle property of the button to null. It will remove the default focus rectangle that appears around the button when it is clicked.

<Button 
    Style="{DynamicResource HeaderButtonStyle}" 
    Grid.Column="0" 
    Background="Transparent" 
    Width="Auto" 
    BorderBrush="Transparent" 
    Click="MinButton_Click"
    FocusVisualStyle="{x:Null}">
    <StackPanel Orientation="Horizontal">
        <Image RenderOptions.BitmapScalingMode="HighQuality" Source="/Resources/icons8-minimize-window-100.png"/>
    </StackPanel>
</Button>

If you want to apply this to all buttons in your application, you can define a style in your App.xaml file.

<Application.Resources>
    <Style TargetType="Button">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    </Style>
</Application.Resources>

All buttons in your application will not show the focus rectangle when clicked. If you want to apply this style to only specific buttons, you can define a style with a key and apply it to the buttons as needed:

<Window.Resources>
    <Style x:Key="NoFocusButtonStyle" TargetType="Button">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    </Style>
</Window.Resources>

<!-- apply the style to a button -->
<Button 
    Style="{StaticResource NoFocusButtonStyle}" 
    Grid.Column="0" 
    Background="Transparent" 
    Width="Auto" 
    BorderBrush="Transparent" 
    Click="MinButton_Click">
    <StackPanel Orientation="Horizontal">
        <Image RenderOptions.BitmapScalingMode="HighQuality" Source="/Resources/icons8-minimize-window-100.png"/>
    </StackPanel>
</Button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading