10

How to disable border of WPF button when I click it?

I have create button like below, everything work fine except when I click on the button.

<Button Background="Transparent" BorderBrush="Transparent">
    <Button.Content>
        <StackPanel>
            <Image Source="xxx.png" />
            <TextBlock Text="Change Password" />
        </StackPanel>
    </Button.Content>
</Button>

When I click the button, it has border like below.

alt text http://www.freeimagehosting.net/uploads/8ece306bd4.png

I try to create style for FocusVisualStyle of the button but it don't work as I expect, this problem also occur when I set IsDefault="True" too.

2 Answers 2

13

I know this is an old question, but felt I could answer.

If I understand the problem correctly, after you click the button and move on, a border remains around it. When you click on some other item, like a TextBox or another Button, the border disappears.

This "border" is the "focus" indicator.

To prevent this, set "Focusable" to "False" on the Button:

<Button Background="Transparent" BorderBrush="Transparent" Focusable="False">
    <Button.Content>
        <StackPanel>
            <Image Source="xxx.png" />
            <TextBlock Text="Change Password" />
        </StackPanel>
    </Button.Content>
</Button>
Sign up to request clarification or add additional context in comments.

3 Comments

I was looking for this...thnx
Well this answer is just crazy. Everyone knows simple solutions in WPF require 300 lines of code and a dependency property
That's not a good solution for people who navigate with the keyboard, since this won't allow you to focus a button with the tabulator.
10

You may have to change the button template, this will give you a button with no frame what so ever, but also without any press or disabled effect:

In your Window.Resources element:

<Style TargetType="Button" x:Key="TransparentButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Background="Transparent">
                   <ContentPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And the button:

<Button Style="{StaticResource TransparentButton}">
    <Button.Content>
        <StackPanel>
            <Image Source="xxx.png" />
            <TextBlock Text="Change Password" />
        </StackPanel>
    </Button.Content>
</Button>

Now, if you need a little more visual feedback start with this template:

http://msdn.microsoft.com/en-us/library/ms753328.aspx

and remove things until you get what you want.

Don't forget to add a transparent background to your elements, if you don't have one, or have a null background the transparent area inside teh button will not be clickable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.