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

  • SEARCH
  • Home
  • 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 8848395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:24:49+00:00 2026-06-14T12:24:49+00:00

I want to create a custom Button inside WPF. Of course, the button will

  • 0

I want to create a custom Button inside WPF. Of course, the button will be an UserControl and it will contain many visual elements (like stroke, highlight, shadow, glow, images etc.).

The problem is that if I use DependencyProperties and bind them in XAML I won’t be able to see the result at DesignTime (I’ve tried to implement IsInDesignMode method but for a certain reason that I can’t understand my VS just crashes when I use this method on UserControls, otherwise it works just fine) and this is definitely not good.

So I am thinking about not using XAML at all and do all my work in the code behind.

What do you guys think?

  • 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-06-14T12:24:51+00:00Added an answer on June 14, 2026 at 12:24 pm

    Like you, when I was getting started and wanted to understand how / what was going on and working with templates, it took a lot of trial and error. Hopefully my research and some step-by-step components can help you customize to your liking and KNOWING where things are coming from.

    First, when trying to understand how a new “template style” will work, I created a simple stand-alone WPF app (“AMS”) for my Any Manipulating Styles. This way, I don’t have to wait forever to see what something will look like during trial / error with the rest of my primary project and themes.

    From that, I created a new WPF Window called “TestingStyles”. Save/Compile, run, no problem.

    Now, in the “VIEW CODE” of the TestingStyles window, I have put whatever I am playing with for a custom class… To help show the step-by-step, I’ve created the following:

    namespace AMS
    {
        /// <summary>
        /// Interaction logic for TestingStyles.xaml
        /// </summary>
        public partial class TestingStyles : Window
        {
            public TestingStyles()
            {
                InitializeComponent();
            }
        }
    
        // Enumerator for a custom property sample...
        public enum HowToShowStatus
        {
            ShowNothing,
            ShowImage1
        }
    
    
        public class YourCustomButtonClass : Button
        {
            public YourCustomButtonClass()
            {
                // auto-register any "click" will call our own custom "click" handler
                // which will change the status...  This could also be done to simplify
                // by only changing visibility, but shows how you could apply via other
                // custom properties too.
                Click += MyCustomClick;
            }
    
            protected void MyCustomClick(object sender, RoutedEventArgs e)
            {
                if( this.ShowStatus == HowToShowStatus.ShowImage1 )
                    this.ShowStatus = HowToShowStatus.ShowNothing;
                else
                    this.ShowStatus = HowToShowStatus.ShowImage1;
            }
    
    
            public static readonly DependencyProperty ShowStatusProperty =
                  DependencyProperty.Register("ShowStatus", typeof(HowToShowStatus),
                  typeof(YourCustomButtonClass), new UIPropertyMetadata(HowToShowStatus.ShowNothing));
    
            public HowToShowStatus ShowStatus
            {
                get { return (HowToShowStatus)GetValue(ShowStatusProperty); }
                set { SetValue(ShowStatusProperty, value); }
            }
        }
    
    }
    

    As you can see, the custom “Button” class, I have at the bottom outside the default TestingStyles : Window declaration… so its all in the same “Project”.

    In this XAML sample, I make reference to a “TaskComplete.png” graphic file (which should just for sample purposes, add directly to the project… Even if a simple smiley face for sample purposes).
    So, create such a simple .png file… even by using Microsoft Paint and drawing a circle with eyes and smile. Save into the project at the root (get into pathing stuff later, get it working first).

    Save and recompile the project, so the project knows publicly what the new “class” (button) is when you start to define the XAML template.

    Now, back to the TestingStyles designer and get it into split screen so you can see both the designer and the XAML markup… and Just replace with the following…

    <Window x:Class="AMS.TestingStyles"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:my="clr-namespace:AMS"
            Title="TestingStyles" Height="300" Width="300" >
    
        <Window.Resources>
            <!-- Build a "Style" based on an anticpated target control type of YourCustomButtonClass.
                per the "my:" reference, the "my" is an "alias" to the xmlsn:my in the declaration above,
                so the XAML knows which library to find such control.  In this case, I've included within
                the actual forms's 'View Code' as a class at the bottom.  
    
                As soon as you assign an "x:Key" reference, its like its telling XAML to make this a PRIVATE
                style so you don't reference it explicitly (yet)
            -->
            <Style TargetType="my:YourCustomButtonClass" x:Key="keyYourCustomButtonClass">
                <!-- put whatever normal "settings" you want for your common look / feel, color -->
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Padding" Value="0,0,1,1"/>
                <Setter Property="Width" Value="100" />
                <Setter Property="Height" Value="30" />
    
                <!-- Now, for the template of the button.  Things can get really crazy here
                  as you are now defining what you want the "button" to look like, borders, 
                  content, etc. In this case, I have two borders to give the raise/sunken effect 
                  of a button and it has its own colors -->
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button" >
                            <!-- The x:Name references used during triggers to know what it is "applying" changes to -->
                            <Border x:Name="BorderTopLeft"
                                    BorderBrush="Gainsboro" 
                                    BorderThickness="0,0,1.5,1.5">
    
                                <Border x:Name="BorderBottomRight"
                                    BorderBrush="Gray" 
                                    BorderThickness="1.5,1.5,0,0">
                                    <!-- Now, what control  type do you want the button to have... 
                                        Ex: You could use a grid (as I have here), stack panels, etc -->
                                    <Grid Background="LightBlue" >
                                        <!-- I'm defining as two columns wide, one row tall.  
                                            First column fixed width 20 pixels example for an image -->
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="20px" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
    
                                        <!-- Now, create the controls I want available within my "template".
                                            when assigned with "x:Name", thats like a property withing the template
                                            that triggers can associate and update to. -->
                                        <Image x:Name="btnImage" 
                                            Grid.Row="0" Grid.Column="0"
                                            Stretch="None"
                                            VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
                                            Source="TaskComplete.png"
                                            Visibility="Visible" />
    
                                        <!-- and also have the text for the button to show the user -->
                                        <TextBlock x:Name="txtNewBtn" 
                                            Grid.Row="0" Grid.Column="1"
                                            Padding="5"
                                            HorizontalAlignment="Left"
                                            VerticalAlignment="Center"
                                        Text="{TemplateBinding Content}" />
                                        <!-- The "{TemplateBinding Content}" means to set the text based on 
                                            the "CONTENT" property of the original button and not use a fixed value -->
                                    </Grid>
                                </Border>
                            </Border>
                            <!-- Now, some triggers for the button itself... some can be property based, others data-based -->
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsPressed" Value="true">
                                    <!-- What properties do we want to change when user CLICKS
                                        on the button, give the "EFFECT" of click down/up by
                                        changing the "Margin" and border thicknesses...  -->
                                    <Setter Property="Margin" Value="1,1,0,0"/>
                                    <!-- Notice the "TargetName" below referring to the x:Name I've applied in template above 
                                        so when the user clicks on the button, it changes the border thickness properties of
                                        each to give the effect of a normal button clicking.  I'm widening one border, shrinking other -->
                                    <Setter TargetName="BorderTopLeft" Property="BorderThickness" Value="2.5,2.5,0,0"/>
                                    <Setter TargetName="BorderBottomRight" Property="BorderThickness" Value="0,0,.5,.5"/>
                                </Trigger>
    
                                <!-- Here, I have a custome property on the class for "ShowStatus".  The binding is to itself
                                    regardless of how many instances of this type of "button" are on a given form 
                                    First trigger happens when the value is changed to "ShowNothing", but can also change 
                                    when set to "ShowImage1" or other as you may need applicable
                                -->
                                <DataTrigger Binding="{Binding Path=ShowStatus, RelativeSource={RelativeSource Self}}" Value="ShowNothing">
                                    <Setter TargetName="btnImage" Property="Visibility" Value="Hidden"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=ShowStatus, RelativeSource={RelativeSource Self}}" Value="ShowImage1">
                                    <Setter TargetName="btnImage" Property="Visibility" Value="Visible"/>
                                </DataTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    
            <!-- NOW, we can expose any instance of "YourCustomButtonClass" button to use the style based on definition above 
                any instance of such YourCustomButtonClass will automatically reflect this style / look -->
            <Style TargetType="my:YourCustomButtonClass" BasedOn="{StaticResource keyYourCustomButtonClass}" />
    
        </Window.Resources>
    
        <Grid>
            <my:YourCustomButtonClass Content="Button" VerticalAlignment="Top" ShowStatus="ShowImage1" />
        </Grid>
    </Window>
    

    This should give you a great jump-start to defining your own templates and how the elements start to tie together. Once this sample is running, as you change any colors, margins, padding, etc to the template, you’ll immediately see the visual impact that component has on the control.

    Have fun and don’t bang your head too much against the wall…

    BTW, once this is working, then you can take the style element stuff within the

    <Window.Resources>
    </Window.Resources> 
    

    and put it into a Windows Resource Dictionary to make it global to your project instead of just this test form.

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

Sidebar

Related Questions

I want to create a custom style for an activity that will look like
I want to create custom WPF control that has a single child control inside.
I want to create Custom UISwitch button, what will i do?
I want to create custom button and I need it to be circle. How
I want put a button on android edittextpreference. I create a custom editextpreference: public
I want to create a custom set that will automatically convert objects into a
I want to create a custom button in ActionScript. This is my code: import
I want to create my custom pin Start Button to win phone 7. For
I want to create custom templated control which controls inside of template coluld be
I would like to make a custom button, that will display 3 data bound

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.