Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7609759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:15:05+00:00 2026-05-31T01:15:05+00:00

i have button with this style: <Style TargetType=Button x:Key=btnStyle> <Setter Property=Template> <Setter.Value> <ControlTemplate TargetType=Button>

  • 0

i have button with this style:

<Style TargetType="Button" x:Key="btnStyle">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Image x:Name="img" Source="{TemplateBinding Tag}" Margin="{TemplateBinding Margin}"
                                 Stretch="None"></Image>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsPressed" Value="true">
                                    <Setter TargetName="img" Property="Source" Value="{DynamicResource imgClose_P}"/>
                                 </Trigger>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter TargetName="img" Property="Source" Value="{DynamicResource imgClose_H}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

As you can see i’m binding the ImageSource to the Tag property of the button.
And in the Tag property i’m binding it to a ResourceDictionary that store this bitmap:

<BitmapImage x:Key="imgClose_N" UriSource="..\AppImages\mainWindow\TopBanner\CloseButton_sN.png" />

this gives me the ability to use this “Imagebutton” all over the application with different background images and one template.
the problem is how to keep this generic approach with triggers?
i would like the IsMouseOver trigger to change the background image but to bind it to some property of the control and not to write it hard coded in the control template.
how can this be done ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:15:06+00:00Added an answer on May 31, 2026 at 1:15 am

    As you already suggest by calling it an “Imagebutton”, you may derive from Button and define some image properties to bind to, e.g. BackgroundImage, MouseOverImage, etc.

    An alternative to deriving from Button would be to use attached properties to set the images and bind to those in your style, but these attached properties would also have to be defined somewhere, which doesn’t make it simpler.

    Here’s an example for the first solution:

    public class ImageButton : Button
    {
        public static readonly DependencyProperty NormalBackgroundImageProperty = DependencyProperty.Register(
            "NormalBackgroundImage", typeof(ImageSource), typeof(ImageButton));
    
        public static readonly DependencyProperty MouseOverBackgroundImageProperty = DependencyProperty.Register(
            "MouseOverBackgroundImage", typeof(ImageSource), typeof(ImageButton));
    
        public static readonly DependencyProperty PressedBackgroundImageProperty = DependencyProperty.Register(
            "PressedBackgroundImage", typeof(ImageSource), typeof(ImageButton));
    
        public ImageSource NormalBackgroundImage
        {
            get { return (ImageSource)GetValue(NormalBackgroundImageProperty); }
            set { SetValue(NormalBackgroundImageProperty, value); }
        }
    
        public ImageSource MouseOverBackgroundImage
        {
            get { return (ImageSource)GetValue(MouseOverBackgroundImageProperty); }
            set { SetValue(MouseOverBackgroundImageProperty, value); }
        }
    
        public ImageSource PressedBackgroundImage
        {
            get { return (ImageSource)GetValue(PressedBackgroundImageProperty); }
            set { SetValue(PressedBackgroundImageProperty, value); }
        }
    }
    

    and an appropriate style below. Note that this style also has a ContentPresenter for the button’s content, and that it uses regular bindings with RelativeSource={RelativeSource TemplatedParent} instead of TemplateBindings. These are evaluated at runtime.

    <Style TargetType="local:ImageButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:ImageButton">
                    <Grid Margin="{TemplateBinding Margin}">
                        <Image x:Name="img" Source="{Binding NormalBackgroundImage, RelativeSource={RelativeSource TemplatedParent}}" Stretch="None"/>
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="img" Property="Source" Value="{Binding MouseOverBackgroundImage, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="img" Property="Source" Value="{Binding PressedBackgroundImage, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    You would have to define or replace the XAML namespace local with a mapping to the namespace/assembly that contains the class ImageButton.

    The button could then be use like this:

    <local:ImageButton
        Margin="10"
        NormalBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
        PressedBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg"
        MouseOverBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"
        Content="Click Me"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom control for ImageButton as <Style TargetType={x:Type Button}> <Setter Property=Template>
I have the following xaml: <DockPanel> <DockPanel.Resources> <Style TargetType=Button> <Style.Triggers> <Trigger Property=IsMouseOver Value=True> <Setter
I have one image submit button like this <input id=enter name=enter type=image value=Login src=images/btn_login.jpg/>
Say I have defined a button with rounded corners. <Style x:Key=RoundButton TargetType=Button> <!-- bla
If I have a button like the one in this image : http://www.freeimagehosting.net/image.php?4cd775814c.png how
For example, I have style <Style TargetType=Button> ... </Style> in file Button.xaml If I
I have a button, this is a unique button, and his style should çn't
I have a template for my list box item. It includes an image button.
I have a DefaultStyle and then another Style defined like this <Style TargetType={x:Type my:CustomButton}
I have a button template shown bellow, in this case when the text is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.