0

I have a button that I am trying to use with certain images. Xaml for the button is below.

    <Button Name="Button1" Grid.Row="1" Grid.Column="1" Background="Transparent" BorderBrush="Transparent" Click="Button1_Click" MouseEnter="Button1_MouseEnter" MouseLeave="Button1_MouseLeave">            
            <Image Name="Button1_Image" Source="/Assets/Button1Passive.png"/>            
    </Button>

The code behind:

private void Button1_Click(object sender, RoutedEventArgs e)
    {
        if (!Button1Active)
        {
            BitmapImage Image1 = new BitmapImage(new Uri("/Assets/Button1Selected.png", UriKind.Relative));
            Button1_Image.Source = Image1;
            Button1Active = true;
        }
        else
        {
            BitmapImage Image1 = new BitmapImage(new Uri("/Assets/Button1Passive.png", UriKind.Relative));
            Button1_Image.Source = Image1;
            Button1Active = false;
        }
    }

    private void Button1_MouseEnter(object sender, MouseEventArgs e)
    {
        if (!Button1Active)
        {
            BitmapImage Image1 = new BitmapImage(new Uri("/Assets/Button1RolledOver.png", UriKind.Relative));
            Button1_Image.Source = Image1;


        }
    }

    private void Button1_MouseLeave(object sender, MouseEventArgs e)
    {
        if (!Button1Active)
        {
            BitmapImage Image1 = new BitmapImage(new Uri("/Assets/Button1Passive.png", UriKind.Relative));
            Button1_Image.Source = Image1;
        }
    }

The problem that I am having is that the image does not take up the entire button and there is a blue box that is around the image when the mouse rolls over it.

WPF Box

I have looked to see if there is another solution and everything that I could find did not allow me to change the image programmatically.

Thanks in advance.

1 Answer 1

4

You should override Button's template

    <Button Name="Button1" Grid.Row="1" Grid.Column="1"
            Background="Transparent" BorderBrush="Transparent"
            Click="Button1_Click" MouseEnter="Button1_MouseEnter"
            MouseLeave="Button1_MouseLeave">   
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <ContentPresenter Content="{TemplateBinding Content}"/>
            </ControlTemplate>
        </Button.Template>
        <Image Name="Button1_Image" Source="/Assets/Button1Passive.png"/>            
    </Button>
Sign up to request clarification or add additional context in comments.

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.