Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / Visual Basic

Declarative Codesnippet Automation with T4 Templates

Rate me:
Please Sign up or sign in to vote.
4.77/5 (15 votes)
20 Apr 2011CPOL15 min read 56.8K   1.4K   36  
This article describes a technique for automating codesnippets which are associated with a class via attributes. This results in a declarative approach to the generation of boiler-plate code.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;

namespace SilverTrack.controls
{
    /// <summary>
    /// Throttle Brake Control displays two rectangles, whose height represents the current throttle
    /// percentage, and the current brake pressure. The template/layout of the control is defined in
    /// Generic.xaml.
    /// </summary>
    public partial class ThrottleBrakeControl : Control
    {
        #region Fields

        private Rectangle _throttleRectange;
        private Rectangle _brakeRectange;

        private TextBlock _throttleLabel;
        private TextBlock _brakeLabel;

        private Border _throttleBorder;
        private Border _brakeBorder;

        #endregion

        #region Properties

        #region Public properties

        /// <summary>
        /// The Throttle Value
        /// </summary>
        public double Throttle
        {
            get { return (double)GetValue(ThrottleProperty); }
            set { SetValue(ThrottleProperty, value); }
        }

        /// <summary>
        /// The Brake Value
        /// </summary>
        public double Brake
        {
            get { return (double)GetValue(BrakeProperty); }
            set { SetValue(BrakeProperty, value); }
        }

        #endregion

        #region Dependency Properties

        /// <summary>
        /// Throttle Dependency Property. Attached to ThrottlePropertyChanged, default value 
        /// </summary>
        public static readonly DependencyProperty ThrottleProperty =
            DependencyProperty.Register("Throttle", typeof(double), typeof(ThrottleBrakeControl),
                new PropertyMetadata(0.0, new PropertyChangedCallback(ThrottlePropertyChanged)));

        /// <summary>
        /// Brake Dependency Property. Attached to BrakePropertyChanged, default value 0.0;
        /// </summary>
        public static readonly DependencyProperty BrakeProperty =
            DependencyProperty.Register("Brake", typeof(double), typeof(ThrottleBrakeControl),
                new PropertyMetadata(0.0, new PropertyChangedCallback(BrakePropertyChanged)));

        #endregion

        #endregion

        #region Methods

        #region Constructors

        public ThrottleBrakeControl()
        {
            DefaultStyleKey = typeof(ThrottleBrakeControl);
        }

        #endregion

        #region Public

        /// <summary>
        /// Sets _throttleRectangle to ThrottleRectangle and _brakeRectangle to BrakeRectange from 
        /// the template.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _throttleRectange = GetTemplateChild("ThrottleRectangle") as Rectangle;
            _throttleBorder = GetTemplateChild("ThrottleContainerBorder") as Border;

            _brakeRectange = GetTemplateChild("BrakeRectangle") as Rectangle;
            _brakeBorder = GetTemplateChild("BrakeContainerBorder") as Border;

            _brakeLabel = GetTemplateChild("BrakeLabel") as TextBlock;
            _throttleLabel = GetTemplateChild("ThrottleLabel") as TextBlock;
        }

        #endregion

        #region Private/Internal

        /// <summary>
        /// Causes the throttle rectangle height to update.
        /// </summary>
        private static void ThrottlePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ThrottleBrakeControl control = d as ThrottleBrakeControl;
            control.OnThrottlePropertyChanged(e);
        }

        private void OnThrottlePropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            _throttleLabel.Text = Throttle.ToString("0.0");
            _throttleRectange.Height = (_throttleBorder.ActualHeight / 100) * Throttle;
        }

        /// <summary>
        /// Causes the brake rectangle height to update.
        /// </summary>
        private static void BrakePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ThrottleBrakeControl control = d as ThrottleBrakeControl;
            control.OnBrakePropertyChanged(e);
        }
        private void OnBrakePropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            _brakeLabel.Text = Brake.ToString("0.0");
            _brakeRectange.Height = (_brakeBorder.ActualHeight / 100) * Brake;
        }

        #endregion

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.

I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.

I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.

Visit my blog - Colin Eberhardt's Adventures in .NET.

Follow me on Twitter - @ColinEberhardt

-

Comments and Discussions