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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:16:16+00:00 2026-05-13T17:16:16+00:00

I am learning about control templates in WPF and checking out how to replace

  • 0

I am learning about control templates in WPF and checking out how to replace the button look with custom template styles. I see that to make a circle button, a Ellipse has to be defined with the same height and width.

<Style x:Key="Button2" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <Ellipse Fill="LightGreen" Width="80" Height="80"/>
                    <ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Control.Margin" Value="10"/>
</Style>

Of course, that only forces all buttons using that style to have a circle with a diameter of 80 pixels, regardless of how the button is resized. I’d like for the circle to take on the shorter of the height/width values, so that it can dynamically scale according to the button sizing.

However i have not read any material that teaches how this can be done in pure XAML template? It seems that some code-behind is required to achieve this effect?

  • 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-13T17:16:16+00:00Added an answer on May 13, 2026 at 5:16 pm

    This is where TemplateBinding comes in (TemplateBinding is used inside control templates and is used to retrieve values from the templated control, in this case the Button).

    <Ellipse Fill="LightGreen" 
        Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}"/>
    

    Note that this is a shorter form of using:

    {Binding ActualWidth, RelativeSource={RelativeSource TemplatedParent}}
    

    The TemplateBinding markup extension is just optimized for only TemplatedParent bindings.

    That said, if you wanted it to be a circle only, if your ellipse was the smaller of W/H, then your content will easily flow out of it, which I doubt is what you actually want..? I had thought of using a multi value converter to do that, but you can’t bind to the converter parameter, so that’s out.

    In that case, an attached behavior would work, but it’s not pretty.

    <Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:WpfApplication1"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="Window1" Height="300" Width="300">
    
        <Grid>
            <Button Content="Yo!" Width="50" Height="30">
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <Ellipse Fill="LightGreen" local:ConstrainWidthHeight.ConstrainedWidth="{TemplateBinding ActualWidth}" local:ConstrainWidthHeight.ConstrainedHeight="{TemplateBinding ActualHeight}"/>
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </Grid>
    </Window>
    

    …and the attached behavior:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    
    namespace WpfApplication1 {
        public class ConstrainWidthHeight {
            public static readonly DependencyProperty ConstrainedWidthProperty =
                DependencyProperty.RegisterAttached( "ConstrainedWidth", typeof( double ), typeof( ConstrainWidthHeight ), new PropertyMetadata( double.NaN, OnConstrainValuesChanged ) );
            public static readonly DependencyProperty ConstrainedHeightProperty =
                DependencyProperty.RegisterAttached( "ConstrainedHeight", typeof( double ), typeof( ConstrainWidthHeight ), new UIPropertyMetadata( double.NaN, OnConstrainValuesChanged ) );
    
            public static double GetConstrainedHeight( FrameworkElement obj ) {
                return (double) obj.GetValue( ConstrainedHeightProperty );
            }
    
            public static void SetConstrainedHeight( FrameworkElement obj, double value ) {
                obj.SetValue( ConstrainedHeightProperty, value );
            }
    
            public static double GetConstrainedWidth( FrameworkElement obj ) {
                return (double) obj.GetValue( ConstrainedWidthProperty );
            }
    
            public static void SetConstrainedWidth( FrameworkElement obj, double value ) {
                obj.SetValue( ConstrainedWidthProperty, value );
            }
    
            private static void OnConstrainValuesChanged( object sender, DependencyPropertyChangedEventArgs e ) {
                FrameworkElement element = sender as FrameworkElement;
                if( element != null ) {
                    double width = GetConstrainedWidth( element );
                    double height = GetConstrainedHeight( element );
    
                    if( width != double.NaN && height != double.NaN ) {
                        double value = Math.Min( width, height );
    
                        element.Width = value;
                        element.Height = value;
                    }
                }
            }
        }
    }
    

    Okay, now the reason why using an attached behavior is required (AFAICT anyway), is that in order to center the ellipse (in a non-square/non-circle scenario), you need the HorizontalAlignment and VerticalAlignment to be able to take effect. The default value of both is Stretch, and when an explicit Width/Height is set, it behaves like Center.

    With Stretch=”Uniform” on, your Ellipse will always physically take up the whole space, it’s only the drawing of the Ellipse that will be constrained. Using this, your drawn Ellipse figure will always start at the top left. So in this case if your button is Wider than it is tall, the drawn portion of the Ellipse won’t get centered along with the text.

    This code is a good example of what you are probably not looking for:

    <Ellipse Height="{TemplateBinding ActualHeight}" Width="{TemplateBinding ActualWidth}" Fill="LightGreen" Stretch="Uniform" />
    

    …and the button using it (with a non-square width/height):

    <Button Content="YO!" Style="{StaticResource Button2}" Width="120" Height="53" VerticalAlignment="Top"></Button>
    

    Looks like this:

    Ugly http://www.freeimagehosting.net/uploads/84e62c4982.png

    … compared to this with the attached property option:

    alt text http://www.freeimagehosting.net/uploads/40755babcd.png

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

Sidebar

Ask A Question

Stats

  • Questions 288k
  • Answers 288k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The only workaround I'm aware of is using dyuproject rather… May 13, 2026 at 5:16 pm
  • Editorial Team
    Editorial Team added an answer Main code: Step 1: Sort strings by length, shortest to… May 13, 2026 at 5:16 pm
  • Editorial Team
    Editorial Team added an answer I suggest you read Search Engine versus DBMS. From what… May 13, 2026 at 5:16 pm

Related Questions

I am confused with learning about WPF. I see ControlTemplate used to determine how
I would like to accomplish two things during my build process: Run unit tests
I am using SharePoint 2007 Enterprise with Windows Server 2008. I am using VSTS
I am still mostly unfamiliar with Inversion of Control (although I am learning about
I am investigating the use of web frameworks with my Java web-app. My basic

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.