Click here to Skip to main content
15,891,567 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.6K   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.
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="SilverTrack.App"
             xmlns:primitives="clr-namespace:Visiblox.Charts.Primitives;assembly=Visiblox.Charts"
             xmlns:charts="clr-namespace:Visiblox.Charts;assembly=Visiblox.Charts"
             xmlns:telemetryUtil="clr-namespace:SilverTrack.Util">
    <Application.Resources>

        <!--Styles-->
        <Style x:Key="WhiteWidgetTextBlockStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontStyle" Value="Italic" />
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="TextAlignment" Value="Center"/>
            <Setter Property="FontFamily" Value="Arial"/>
        </Style>

        <Style x:Key="ValueLabelStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="TextAlignment" Value="Center"/>
            <Setter Property="FontFamily" Value="Arial"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>

        <Style x:Key="PrimarySeriesStyle" TargetType="charts:LineSeries">
            <Setter Property="LineStroke" Value="Black"/>
            <Setter Property="LineStrokeThickness" Value="1.5"/>
            <Setter Property="AreaFill" Value="Red"/>
            <Setter Property="ShowArea" Value="True"/>
        </Style>

        <Style x:Key="SecondarySeriesStyle" TargetType="charts:LineSeries">
            <Setter Property="LineStroke" Value="Black"/>
            <Setter Property="LineStrokeThickness" Value="1.5"/>
        </Style>

        <Style x:Key="HeaderLabelStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="TextAlignment" Value="Left"/>
            <Setter Property="FontFamily" Value="Arial"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="5,0,0,0"/>
        </Style>

        <Style TargetType="Button" x:Name="ButtonStyle">
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Background" Value="#FF1F3B53"/>
            <Setter Property="Margin" Value="3,0,0,0"/>
            <Setter Property="Padding" Value="3"/>
            <Setter Property="BorderThickness" Value="2"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="FontFamily" Value="Arial"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                                       Storyboard.TargetProperty="Border.Background"
                                                                       BeginTime="00:00:00" Duration="00:00:00">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource ButtonMouseOverBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                                       Storyboard.TargetProperty="Border.Background"
                                                                       BeginTime="00:00:00" Duration="00:00:00">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource ButtonPressedBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                                       Storyboard.TargetProperty="Border.Opacity"
                                                                       BeginTime="00:00:00" Duration="00:00:00">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="0.3"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>


                            <Border x:Name="border" BorderThickness="2" BorderBrush="Black"
                                            HorizontalAlignment="Stretch"
                                            Background="{StaticResource ButtonBrush}">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="Button" x:Name="CloseButtonStyle">
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Margin" Value="3,0,0,0"/>
            <Setter Property="Padding" Value="3"/>
            <Setter Property="BorderThickness" Value="2"/>
            <Setter Property="BorderBrush" Value="#FFD70E08"/>
            <Setter Property="FontFamily" Value="Arial"/>
            <Setter Property="FontWeight" Value="ExtraBold"/>
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                                       Storyboard.TargetProperty="Border.Background"
                                                                       BeginTime="00:00:00" Duration="00:00:00">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource RedButtonMouseOverBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                                       Storyboard.TargetProperty="Border.Background"
                                                                       BeginTime="00:00:00" Duration="00:00:00">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource RedButtonPressedBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>

                            <Border x:Name="border" BorderThickness="2" BorderBrush="#FFD70E08"
                                            HorizontalAlignment="Stretch"
                                            Background="{StaticResource RedButtonBrush}">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!-- Style for a ComboBox's ToggleButton -->
        <Style x:Name="ComboBoxToggleStyle" TargetType="ToggleButton">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Background" Value="#FF1F3B53"/>
            <Setter Property="Padding" Value="3"/>
            <Setter Property="FontFamily" Value="Arial"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                                       Storyboard.TargetProperty="Border.Background"
                                                                       BeginTime="00:00:00" Duration="00:00:00">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource ButtonMouseOverBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                                       Storyboard.TargetProperty="Border.Background"
                                                                       BeginTime="00:00:00" Duration="00:00:00">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource ButtonPressedBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                                       Storyboard.TargetProperty="Border.Opacity"
                                                                       BeginTime="00:00:00" Duration="00:00:00">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="0.3"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>

                            <Border x:Name="border" BorderBrush="Black"
                                                       HorizontalAlignment="Stretch" Width="Auto"
                                                       BorderThickness="2"
                                                       Background="{StaticResource ButtonBrush}">

                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" />
                            </Border>

                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!-- Style for a ComboBox (used in the Browse tab) -->
        <Style x:Name="ComboBoxStyle" TargetType="ComboBox">
            <Setter Property="Background" Value="#111" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BtnArrow"
                                                                       Storyboard.TargetProperty="Path.Opacity"
                                                                       BeginTime="00:00:00" Duration="00:00:00">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="0.2"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>

                            <Border x:Name="ContentPresenterBorder">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition />
                                        <ColumnDefinition Width="20"/>
                                    </Grid.ColumnDefinitions>
                                    <ToggleButton x:Name="DropDownToggle" Style="{StaticResource ComboBoxToggleStyle}"
                                                                      HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Margin="0"
                                                                      Background="{TemplateBinding Background}" Grid.ColumnSpan="2"
                                                                      BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" />
                                    <Path Grid.Column="1" x:Name="BtnArrow" Height="4" Width="8" Stretch="Uniform"
                                                                  Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "
                                                                  Margin="0,0,6,0" HorizontalAlignment="Right">
                                        <Path.Fill>
                                            <SolidColorBrush Color="White"/>
                                        </Path.Fill>
                                    </Path>
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                            IsHitTestVisible="False"
                                                            x:Name="ContentPresenter" Margin="5,0,0,0" >
                                        <TextBlock IsHitTestVisible="False" Text=" "/>
                                    </ContentPresenter>
                                </Grid>
                            </Border>
                            <Popup x:Name="Popup" Margin="0">
                                <Border Height="Auto" HorizontalAlignment="Stretch"
                                                            x:Name="PopupBorder" 
                                                            BorderBrush="Black"
                                                            BorderThickness="2"
                                                            Background="{TemplateBinding Background}">
                                    <ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1">
                                        <ScrollViewer.Background>
                                            <SolidColorBrush Color="{TemplateBinding Background}"/>
                                        </ScrollViewer.Background>
                                        <ItemsPresenter/>
                                    </ScrollViewer>
                                </Border>
                            </Popup>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!--End of styles-->


        <!--Brushes-->

        <SolidColorBrush x:Name="PrimaryYBrush" Color="#FFD70E08"/>
        <SolidColorBrush x:Name="SecondaryYBrush" Color="#FF21C705"/>
        <SolidColorBrush x:Name="ValueLabelBrush" Color="Black"/>

        <ImageBrush x:Name="BackgroundImageBrush" ImageSource="images/bg.png" Stretch="None"/>
        <ImageBrush x:Name="YellowBackgroundImageBrush" ImageSource="images/yellowbg.png" Stretch="None"/>

        <LinearGradientBrush x:Name="ButtonBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0.5" Color="#707070" />
            <GradientStop Offset="0.501" Color="#000" />
        </LinearGradientBrush>
        <LinearGradientBrush x:Name="ButtonMouseOverBrush"  StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0.5" Color="#444444" />
            <GradientStop Offset="0.501" Color="#000" />
        </LinearGradientBrush>
        <LinearGradientBrush x:Name="ButtonPressedBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0.5" Color="#333" />
            <GradientStop Offset="0.501" Color="#000" />
        </LinearGradientBrush>
        <LinearGradientBrush x:Name="ButtonDisabledBrush" StartPoint="0,0" EndPoint="0,1" Opacity="0.5">
            <GradientStop Offset="0.5" Color="#333" />
            <GradientStop Offset="0.501" Color="#000" />
        </LinearGradientBrush>
        <LinearGradientBrush x:Name="BlackHeader" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0.499" Color="#707070" />
            <GradientStop Offset="0.5" Color="#000" />
        </LinearGradientBrush>


        <LinearGradientBrush x:Name="RedButtonBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0.5" Color="Red" />
            <GradientStop Offset="0.501" Color="#FFD70E08" />
        </LinearGradientBrush>
        <LinearGradientBrush x:Name="RedButtonMouseOverBrush"  StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0.5" Color="#F20056" />
            <GradientStop Offset="0.501" Color="#FFD70E08" />
        </LinearGradientBrush>
        <LinearGradientBrush x:Name="RedButtonPressedBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0.5" Color="Red" />
            <GradientStop Offset="0.501" Color="#FFD70E08" />
        </LinearGradientBrush>


        <LinearGradientBrush x:Name="SpeedometerBackgroundBrush" StartPoint="0.4,0.4" EndPoint="0.8,0.8">
            <GradientStop Offset="0" Color="#404040" />
            <GradientStop Offset="0.01" Color="#000" />
        </LinearGradientBrush>

        <LinearGradientBrush x:Name="ThrottleBackgroundBrush" StartPoint="0.4,0.4" EndPoint="0.8,0.8">
            <GradientStop Offset="0" Color="#404040" />
            <GradientStop Offset="0.01" Color="#000" />
        </LinearGradientBrush>

        <LinearGradientBrush x:Name="SeparatorBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0.499" Color="#333333" />
            <GradientStop Offset="0.5" Color="#000" />
        </LinearGradientBrush>
        <SolidColorBrush x:Key="PathBrush" Color="White"/>


        <!--End of brushes-->


        <!--Templates-->

        <ControlTemplate x:Key="TemplatedChart" TargetType="charts:Chart">
            <Grid x:Name="LayoutRoot">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid Name="PlotArea" Grid.Row="1" Grid.Column="1" primitives:Clip.ToBounds="True" Style="{TemplateBinding PlotAreaStyle}" Background="White">
                    <Border Name="PlotAreaBorder" Canvas.ZIndex="100" BorderThickness="0"/>
                    <Grid Name="SeriesContainer"/>
                    <Grid Name="GridlinesContainer" Canvas.ZIndex="-100"/>
                    <Canvas Name="BehaviourContainer"/>
                    <Grid Name="AnnotationsContainer" />
                </Grid>
                <Grid x:Name="YAxisPrimaryContainer" Grid.Column="0" Grid.Row="1"/>
                <Grid x:Name="YAxisSecondaryContainer" Grid.Column="2" Grid.Row="1"/>
                <Grid x:Name="XAxisPrimaryContainer" Grid.Column="1" Grid.Row="2"/>
            </Grid>
        </ControlTemplate>

        <ControlTemplate x:Key="GChart" TargetType="charts:Chart">
            <Border Margin="0">
                <Grid x:Name="LayoutRoot" Background="Transparent">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid Name="PlotArea" Grid.Row="1" Grid.Column="1" primitives:Clip.ToBounds="True" Style="{TemplateBinding PlotAreaStyle}">
                        <Border Name="PlotAreaBorder" Canvas.ZIndex="100" BorderBrush="LightGray" BorderThickness="0"/>
                        <Grid Name="SeriesContainer"/>
                        <Grid Name="GridlinesContainer" Canvas.ZIndex="-100"/>
                        <Canvas Name="BehaviourContainer"/>
                        <Grid Name="AnnotationsContainer"/>
                    </Grid>
                    <Grid x:Name="YAxisPrimaryContainer" Grid.Column="0" Grid.Row="1" Width="0"/>
                    <Grid x:Name="YAxisSecondaryContainer" Grid.Column="2" Grid.Row="1"/>
                    <Grid x:Name="XAxisPrimaryContainer" Grid.Column="1" Grid.Row="2" Height="0"/>
                </Grid>
            </Border>
        </ControlTemplate>

        <!--End of Templates-->

        <primitives:UIPointConverter x:Key="UIPointConverter" />

        <ControlTemplate TargetType="charts:Trackball" x:Key="CustomTrackball">
            <primitives:ZoomCanvas x:Name="LayoutRoot">
                <Rectangle Fill="Black" Height="10000"  Width="0.5" Margin="0,-5000,0,0"
                           primitives:ZoomCanvas.ElementPosition="{Binding Path=RenderPoint,
                           RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource UIPointConverter}}" />
            </primitives:ZoomCanvas>
        </ControlTemplate>

    </Application.Resources>
</Application>

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