Click here to Skip to main content
14,301,033 members

A Simple Cross Button for WPF

Rate this:
4.89 (16 votes)
Please Sign up or sign in to vote.
4.89 (16 votes)
18 Aug 2011CPOL
A small and simple cross button for your WPF applications.



In this article, I'll show you how to create a very simple control to represent a small cross button - the sort that you see in the tabbed windows or certain controls.


I found that when creating a custom tab control that allowed the user to close the tabs with a small button, a control like this would be useful. However, in later projects, this small control has been surprisingly versatile.

Creating a Custom Control

Let's get started. Create a new Visual Studio Solution and add a WPF Custom Control Library to it. The custom control code is very simple:

/// <summary>
/// The Cross Button is a very simple version
/// of the button that displays as a discrete cross,
/// similar to the buttons at the top of Google Chrome's tabs.
/// </summary>
public class CrossButton : Button
    /// <summary>
    /// Initializes the <see cref="CrossButton"/> class.
    /// </summary>
    static CrossButton()
        //  Set the style key, so that our control template is used.
               new FrameworkPropertyMetadata(typeof(CrossButton)));

Now we are deriving the control from Button; the only thing this code really does is makes sure that the style we use for the CrossButton is the correct style that we will add in our Generic.xaml dictionary.

We actually have no custom code in the control - we could define the whole thing as a style that just re-templates the button. However, in my local version, I have added a few more features - if you have a class like this, you can extend yours too.


The XAML in the Generic.xaml file is basic as well:




    <Style TargetType="{x:Type local:CrossButton}">

Generic.xaml is a resource dictionary, we need a reference to the namespace we have defined in the control in. The only thing we have in the resource dictionary is the style for the cross button.

Note: If you want to implement this control simply as a style, just set the target type to 'Button' and give the style a key - then apply the style to any button.

Moving on, we'll need a few resources for the button.

<!-- Brushes we use for the control. -->
    <SolidColorBrush x:Key="NormalBackgroundBrush" Color="#00000000" />
    <SolidColorBrush x:Key="NormalBorderBrush" Color="#FFFFFFFF" />
    <SolidColorBrush x:Key="NormalForegroundBrush" Color="#FF8f949b" />

    <SolidColorBrush x:Key="HoverBackgroundBrush" Color="#FFc13535" />
    <SolidColorBrush x:Key="HoverForegroundBrush" Color="#FFf9ebeb" />

    <SolidColorBrush x:Key="PressedBackgroundBrush" Color="#FF431e20" />
    <SolidColorBrush x:Key="PressedBorderBrush" Color="#FF110033" />
    <SolidColorBrush x:Key="PressedForegroundBrush" Color="#FFf9ebeb" />

These are the brushes we'll use to paint the button in its various states.

<!-- Simple properties that we set. -->
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Focusable" Value="False" />

This is such a small button that it'll help to have the cursor as a hand so that it is obvious it is clickable. Also, by setting the Focusable style to False, we stop the user from tabbing to the control which in most circumstances would be a bit odd.

Here's where it gets more interesting:

<!-- The control template. -->
<Setter Property="Template">
<ControlTemplate TargetType="{x:Type Button}"> 

This is where we are going to change the control's template. The control template is what is used to draw the control - by changing the template, we can completely change how we draw the control.

<Grid Background="Transparent">

<!-- The background of the button, as an ellipse. -->
<Ellipse x:Name="backgroundEllipse" />

<!-- A path that renders a cross. -->
<Path x:Name="ButtonPath"


      Stroke="{StaticResource NormalForegroundBrush}"







The cross button is a simple looking button - a small cross with a red circle behind it when the mouse is over it. We put the ellipse and path (which will draw the cross) in a grid, drawing one on top of the other.

We have the path, but we'll need to define the geometry for it:

                    <PathFigure StartPoint="0,0">
                        <LineSegment Point="25,25"/>
                    <PathFigure StartPoint="0,25">
                        <LineSegment Point="25,0"/>

The geometry is very simple - two line segments! We can close the path and the grid and now move onto the more interactive features. We'll use triggers to change colours as the mouse moves over the control.

<!-- The triggers. -->
    <Trigger Property="IsMouseOver" Value="True">
        <Setter TargetName="backgroundEllipse" Property="Fill"

                Value="{StaticResource HoverBackgroundBrush}" />
        <Setter TargetName="ButtonPath" Property="Stroke" 

                Value="{StaticResource HoverForegroundBrush}"/>

The first trigger is fired when the mouse moves over - simply changing the colour of the background ellipse and the button path.

<Trigger Property="IsEnabled" Value="false">
    <Setter Property="Visibility" Value="Collapsed"/>

If the control isn't enabled, we'll just hide it - it's a very discrete little button.

                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="backgroundEllipse" Property="Fill"

                                    Value="{StaticResource PressedBackgroundBrush}" />
                            <Setter TargetName="backgroundEllipse" Property="Stroke"

                                    Value="{StaticResource PressedBorderBrush}" />
                            <Setter TargetName="ButtonPath" Property="Stroke" 

                                    Value="{StaticResource PressedForegroundBrush}"/>

We use the final trigger to colour the control black and white when it is pressed. After this, we close all of the tags and we're done with the dictionary.


The example application shows how to use the control in a data template to remove items from a list:


This is just one use for the control - below is another (not in the example):


If anyone would find the above control useful, let me know and I'll write an article about it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dave Kerr
Software Developer
United Kingdom United Kingdom
Follow my blog at and find out about my charity at

Comments and Discussions

GeneralMy vote of 5 Pin
Stef Keiman10-May-15 1:05
memberStef Keiman10-May-15 1:05 
GeneralAlmost sad... Pin
themightylechuck17-Dec-12 8:21
memberthemightylechuck17-Dec-12 8:21 
GeneralRe: Almost sad... Pin
Dave Kerr17-Dec-12 19:39
mvaDave Kerr17-Dec-12 19:39 
Big Grin | :-D ! Well I've got to admit, now you've got me interested - fancy sending over a before and after screenshot? If it's an application you're promoting on the internet I could always link to it from this article and my blog!

Anyway, very pleased you've found it useful!

QuestionGood but Pin
Ray Guan9-Nov-12 19:23
memberRay Guan9-Nov-12 19:23 
AnswerRe: Good but Pin
Dave Kerr17-Dec-12 19:38
mvaDave Kerr17-Dec-12 19:38 
AnswerRe: Good but Pin
Jon Peterson24-Feb-16 7:03
memberJon Peterson24-Feb-16 7:03 
QuestionMy vote of 5 Pin
Filip D'haene21-Aug-11 6:04
memberFilip D'haene21-Aug-11 6:04 
AnswerRe: My vote of 5 Pin
Dave Kerr17-Dec-12 19:37
mvaDave Kerr17-Dec-12 19:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 18 Aug 2011

Tagged as


51 bookmarked