WPF image button control

wpf button with image and text
wpf button image from resource
wpf image button style
image button in wpf xaml
wpf button background image
wpf custom button with image
wpf button image programmatically
wpf button image size

Long time is now that I have been searching for a quality WPF image button control. I need the button to consist of an image only and let me set image options for hovering and pressing. All current solutions on SO and the rest of the web include CustomTemplate based solutions that are not very friendly (i.e. TriState Button).

Maybe there is a set of controls like Modern UI or MahApps that someone could point me to that have this kind of button?

EDIT: This control can be reused throughout a project as many times as you need it acts exactly as any other control would and is used the same way as any other control library. There is no Border effect and you can add whatever hover effect you want. This is how it works.

Add a folder named CustomControls to your solution.

Create a custom control(WPF) named ImageButton in that folder. The custom control code goes in there.

Now a folder named Themes should have been created in your project with a resourcedictionary named generic.xaml inside it. Open it and use the resourcedictionary code.

Now add this to your window tag

xmlns:b="clr-namespace:MyProject.CustomControls"

Now you can use the control in that window as many times as you'd like just like a regular button by using the tag structure at the end of the answer.

Here is one that I made you can set the Image source, height and width all from your main window and all standard button events are there.

Here is the custom control

namespace MyProject.CustomControls
{
public class ImageButton : Button
{
     public static DependencyProperty SourceProperty =
        DependencyProperty.Register(
            "Source",
            typeof(Uri),
            typeof(ImageButton));

    static ImageButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
    }

    public Uri Source
    {
        get { return (Uri)GetValue(SourceProperty); }
        set { SetValue(SourceProperty, value); }
    }
}

}

Here is the style in the resourcedictionary, you can add mouseover event and a button.pressed event in the triggers section or remove the triggers and set them on your window.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:b="clr-namespace:MyProject.CustomControls">

<Style x:Key="{x:Type b:ImageButton}" TargetType="{x:Type b:ImageButton}">
    <Setter Property="Height" Value="{Binding Path=Height, RelativeSource={RelativeSource TemplatedParent}}"/>
    <Setter Property="Width" Value="{Binding Path=Width, RelativeSource={RelativeSource TemplatedParent}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type b:ImageButton}">
                <Grid x:Name="LayoutGrid">
                    <Image x:Name="_Image" HorizontalAlignment="Center" VerticalAlignment="Center" Width="{TemplateBinding Width}"
                           Source="{Binding Path=Source, RelativeSource={RelativeSource TemplatedParent}}" Height="{TemplateBinding Height}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="" TargetName="" Value=""/>
                    </Trigger>
                    <Trigger Property="Button.IsPressed" Value="True">
                        <Setter Property="" TargetName="" Value=""/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

To use it in your project add the namespace to the window and then the tag will be like this

<b:ImageButton Source="Image.png" Height="50" Width="50" Click="do_Something"/>

How to: Create a Button That Has an Image, The following example creates two Button controls. One Button contains text and the other contains an image. The image is in a folder called´┐Ż Abhi Sankrityayan. In this tutorial you will learn to create an image button control in WPF. Also you will learn about putting image inside button control using stack panel, how to change background color of button in WPF, how to change border brush of button control in WPF.

Here you have: https://imagebuttonwpf.codeplex.com/releases/view/70356

You'll have to download de code, compile it, and then use the generated DLL in your project.

Check the code, it's pretty easy to make, and following CJK instructions for the actual control you could've done it yourself in no time!

Image Button Control in WPF, Also you will learn about putting image inside button control using stack panel, how to change Duration: 5:26 Posted: May 7, 2018 In this tutorial you will learn how to put image inside button control in WPF and how to use the Button Template to create an image button control. Tune in FREE to the React Virtual Conference Sep. 11 at 10am ET x

You can try this one

    <Button Background="Transparent" BorderThickness="0">
            <Image Source="Azure Automation.jpg"/>
    </Button>

WPF Button with Image, In WPF, most of the controls can load an image as their content. The following code sample creates two Button controls. One Button contains text and the other contains an image. When a user clicks the Button that has the image, the background and the text of the other Button change. Create a custom control(WPF) named ImageButton in that folder. The custom control code goes in there. Now a folder named Themes should have been created in your project with a resourcedictionary named generic.xaml inside it. Open it and use the resourcedictionary code. Now add this to your window tag. xmlns:b="clr-namespace:MyProject.CustomControls"

Image Button Control in Windows Presentation Foundation, In this tutorial you will learn to create an image button control in WPF. Also you will learn about Duration: 5:26 Posted: Jul 9, 2016 The Image control. The WPF Image control will allow you to display images inside your applications. It's a very versatile control, with many useful options and methods, as you will learn in this article. But first, let's see the most basic example of including an image inside a Window: <Image Source="https://upload.wikimedia.org/wikipedia/commons/3/30/Googlelogo.png" />.

How to put an image into a button control in wpf, In this WPF Tutorial, I will show you how to add image inside the button control. I will give Duration: 3:13 Posted: May 2, 2014 public class ImageButton : Button { static ImageButton () { DefaultStyleKeyProperty.OverrideMetadata ( typeof (ImageButton), new FrameworkPropertyMetadata ( typeof (ImageButton))); } } For setting the image we need a property, which I named Image, but in the WPF Custom Control, we used Dependency Property (for more information, look at wpftutorial.net ).

Putting Image inside Button Control in Windows Presentation , In this tutorial you will learn How to put image inside button control in WPF. How to use the Duration: 4:58 Posted: Jul 10, 2016 In this WPF Tutorial, I will show you how to add image inside the button control. I will give you a demo of image with button.

Comments
  • What is the problem with simply setting an Image as content of the button?
  • How about hover image and undesired border effects of the Button control?
  • I am looking for a more generic control, not a template solution
  • what do you mean by more generic?
  • Library type, nicely wrapped control/dll etc.
  • I need easy of use and re-usability in general.
  • I've edited my answer to show how this can work for you, let me know if you have any questions with it.
  • Says "project is not yet published" when I click on the link
  • True, can't publish it without uploading the code... Mpf! I'll try to find another place to upload it :/
  • Ok, nevermind, I found another control made by other people, there in Codeplex... Updating my answer.
  • No image hover effect this way.
  • @eYe In that case you need to write the template for Button