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 3961304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:57:33+00:00 2026-05-20T02:57:33+00:00

I am a bit new to WPF and XAML, just learning now. I found

  • 0

I am a bit new to WPF and XAML, just learning now.

I found a quick-n-dirty code by some previous developer:

<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
        <Border Name="buttonBorder" Background="{TemplateBinding Background}">
            <Border.Effect>
                <DropShadowEffect Opacity="0.0" />
            </Border.Effect>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="buttonBorder" Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect Opacity="0.8" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsMouseCaptured" Value="true">
                    <Setter TargetName="buttonBorder" Property="Effect">                   
                         <Setter.Value>
                        <DropShadowEffect Opacity="0.8" Direction="135"  
                             ShadowDepth="3" BlurRadius="1" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                   <Setter TargetName="buttonBorder" Property="Background">
                        <Setter.Value>
                        <ImageBrush ImageSource="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}"  />
                        </Setter.Value>
                    </Setter>            
                   <Setter TargetName="buttonBorder" Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect Opacity="0.0"/>
                    </Setter.Value>
                </Setter>     

            </Trigger>
        </ControlTemplate.Triggers>
        </ControlTemplate>

Basically it is just a template for a button with a basic mouseover effect and the image for disabled state bound to Tag (seems an ugly solution).

I want to create a custom button which works pretty the same, but I want to expose two custom properties: NormalImage and DisabledImage. Those properties should be of type string not Uri. I want to use the path to image just “apply.png” and not “pack://application:,,,/Resources/Apply.png”.
I guess, to have such custom properties I need a UserControl with dependency properties?

Basically, I want to use the button as follows:

<MyImageButton NormalImage="apply.png" DisabledImage="apply_disabled.png"/>

Maybe NormalImage/DisabledImage will be bound to something later but that’s unlikely.

I could not find any example which implements such a basic button with custom properties, there are only some fancy buttons and controls online. Maybe I am just using incorrect keywords…

Could anyone point me to the right article or throw some simple piece of code to play with?

WPF is so complicated for beginners, sometimes it just does not work as expected, for example I still do not understand why I can add Trigger tag to ControlTemplate, but I cannot add Trigger tag straight to UserControl…

  • 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-20T02:57:34+00:00Added an answer on May 20, 2026 at 2:57 am

    You could also use a UserControl which is a bit messy since your button will be encapsulated, would look something like this:

    Xaml:

    <UserControl x:Class="Test.ImageButton"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300"
                 DataContext="{Binding RelativeSource={RelativeSource Self}}">
        <Button Name="button" Click="button_Click" Width="50" Height="50">
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Name="buttonBorder">
                        <Border.Effect>
                            <DropShadowEffect Opacity="0.0" />
                        </Border.Effect>
                        <Border.Child>
                            <Image Name="img" Source="{Binding NormalImage}"/>
                        </Border.Child>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="buttonBorder" Property="Effect">
                                <Setter.Value>
                                    <DropShadowEffect Opacity="0.8" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsMouseCaptured" Value="true">
                            <Setter TargetName="buttonBorder" Property="Effect">
                                <Setter.Value>
                                    <DropShadowEffect Opacity="0.8" Direction="135"  
                                 ShadowDepth="3" BlurRadius="1" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="img" Property="Source" Value="{Binding DisabledImage}"/>
                            <Setter TargetName="buttonBorder" Property="Effect">
                                <Setter.Value>
                                    <DropShadowEffect Opacity="0.0"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </UserControl>
    

    Code behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace Test
    {
        /// <summary>
        /// Interaction logic for ImageButton.xaml
        /// </summary>
        public partial class ImageButton : UserControl
        {
            public ImageSource DisabledImage
            {
                get { return (ImageSource)GetValue(DisabledImageProperty); }
                set { SetValue(DisabledImageProperty, value); }
            }
            public static readonly DependencyProperty DisabledImageProperty =
                DependencyProperty.Register("DisabledImage", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));
    
    
            public ImageSource NormalImage
            {
                get { return (ImageSource)GetValue(NormalImageProperty); }
                set { SetValue(NormalImageProperty, value); }
            }
            public static readonly DependencyProperty NormalImageProperty =
                DependencyProperty.Register("NormalImage", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));
    
            public event RoutedEventHandler Click;
    
            public ImageButton()
            {
                InitializeComponent();
            }
    
            private void button_Click(object sender, RoutedEventArgs e)
            {
                if (Click != null)
                {
                    Click(this, e);
                }
            }
        }
    }
    

    Example usage:

            <local:ImageButton x:Name="imgbutton"
                               NormalImage="C:/1.png"
                               DisabledImage="C:/2.png"
                               Click="ImgButton_Click"/>
    

    (Note that the current namespace is Test, you might want to change that; Also i set a fixed size on the internal button, which you might want to remove, just make sure to set the size somewhere since i think it will not use any space at all if you don’t.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A bit new with WPF...It seems like not matter what I change with horizontal
The following bit of code catches the EOS Exception using (var reader = new
I have a bit of code that looks like this: text = reg.Replace(text, new
Still a bit new to the concept of M-V-VM in WPF, here's my problem:
I am a bit new to SVNed code. We are a small team (4
I am a bit new to Perl, but here is what I want to
I'm a bit new to WCF and I don't think I completely understand what
Thanks for reading. I'm a bit new to jQuery, and am trying to make
I'm setting up a new 64 bit machine to run as a build server.
I'm fairly new to the Zend Framework and MVC and I'm a bit confused

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.