Posted by: Zeeshan Amjad | June 16, 2010

Define Control template with code


We already saw an example of defining data template with code here. This time we are going to do the same thing, but with control template. We are going to define the control template of button control. Here is a C# code to define the control template, which has one grid and one ellipse shape.

FrameworkElementFactory ellipse = new FrameworkElementFactory(typeof(Ellipse));
ellipse.SetValue(Ellipse.FillProperty, Brushes.AliceBlue);
ellipse.SetValue(Ellipse.MarginProperty, new Thickness(2));
ellipse.SetValue(Ellipse.StrokeProperty, Brushes.Blue);

FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));
grid.SetValue(Grid.MarginProperty, new Thickness(5));

grid.AppendChild(ellipse);

ControlTemplate ct = new ControlTemplate(typeof(Button));
ct.VisualTree = ellipse;

Then we can apply this template to our button control. It would be something like this.

Button b1 = new Button();
b1.Margin = new Thickness(5);
b1.Width = 100;
b1.Height = 50;
b1.Template = ct;

 

Here is complete C# code of the program.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfButton
{
    public class WpfButton
    {
        [STAThread]
        public static void Main()
        {
            Window win = new Window();
            win.Title = "Button Control Template";
            win.Width = 400;
            win.Height = 300;

            StackPanel sp = new StackPanel();            

            FrameworkElementFactory ellipse = new FrameworkElementFactory(typeof(Ellipse));
            ellipse.SetValue(Ellipse.FillProperty, Brushes.AliceBlue);
            ellipse.SetValue(Ellipse.MarginProperty, new Thickness(2));
            ellipse.SetValue(Ellipse.StrokeProperty, Brushes.Blue);
            
            FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));
            grid.SetValue(Grid.MarginProperty, new Thickness(5));

            grid.AppendChild(ellipse);

            ControlTemplate ct = new ControlTemplate(typeof(Button));
            ct.VisualTree = ellipse;

            Button b1 = new Button();
            b1.Margin = new Thickness(5);
            b1.Width = 100;
            b1.Height = 50;
            b1.Template = ct;

            sp.Children.Add(b1);

            Button b2 = new Button();
            b2.Margin = new Thickness(5);
            b2.Width = 100;
            b2.Height = 50;
            b2.Template = ct;

            sp.Children.Add(b2);

            win.Content = sp;

            Application app = new Application();
            app.Run(win);
        }
    }
}

 

Here is the output of the program.

ButtonControlTemplate

Advertisements

Responses

  1. […] just saw an example of defining control template with code here. In that example we define it using C#. Now we are going to do the same experiment with VC++. In […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: