Click here to Skip to main content
15,886,110 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.5K   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.
<UserControl x:Class="SilverTrack.View.TelemetryView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:view="clr-namespace:SilverTrack.View"
    xmlns:telemetry="clr-namespace:SilverTrack.controls"
    xmlns:telemetryGauge="clr-namespace:SilverTrack.controls.gaugeControl"      
    mc:Ignorable="d"
    d:DesignWidth="1000" d:DesignHeight="500" Foreground="White">

    <Border Background="Black">

        <Grid x:Name="LayoutRoot" Width="960" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="30"/>
                <RowDefinition/>
                <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>

            <Border Grid.Row="0" Padding="5,0,0,0" Background="{StaticResource BlackHeader}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock VerticalAlignment="Center" FontSize="40" FontFamily="Arial" FontWeight="Bold" Text="SILVER" FontStyle="Normal" TextAlignment="Right" Grid.Column="1">
                        <TextBlock.Foreground>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFEFECEC" Offset="0.615" />
                            <GradientStop Color="#FFE2E2E2" Offset="1" />
                            <GradientStop Color="#FFC1C1C1" Offset="0.695" />
                        </LinearGradientBrush>
                        </TextBlock.Foreground>
                        <TextBlock.Effect>
                            <DropShadowEffect Color="Black" Direction="0" BlurRadius="6" Opacity="0.8" ShadowDepth="0"/>
                        </TextBlock.Effect>
                    </TextBlock>
                    <TextBlock VerticalAlignment="Center" FontSize="40" FontFamily="Arial" FontWeight="Bold" Text="TRACK" FontStyle="Italic" Grid.Column="2">
                    <TextBlock.Foreground>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFF89C9C" Offset="0.56" />
                            <GradientStop Color="#FFF29595" Offset="1" />
                            <GradientStop Color="#FFE40000" Offset="0.604" />
                        </LinearGradientBrush>
                    </TextBlock.Foreground>
                        <TextBlock.Effect>
                            <DropShadowEffect Color="Black" Direction="0" BlurRadius="6" Opacity="0.8" ShadowDepth="0"/>
                        </TextBlock.Effect>
                    </TextBlock>
                    <Image Grid.Column="3" Source="/SilverTrack;component/images/flag.png" Stretch="UniformToFill"></Image>
                </Grid>
            </Border>

            <Border Grid.Row="1" Margin="0,3" Background="{StaticResource YellowBackgroundImageBrush}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <!--Right buttons stack panel-->
                    <StackPanel Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Right">
                        <TextBlock x:Name="TimeTextBlock" VerticalAlignment="Center" Padding="0,0,0,0"
                                   Foreground="Black" FontWeight="Bold" FontSize="14" FontFamily="Arial" Text="{Binding Path=TimeText}"/>
                        <Button x:Name="PlayButton" Grid.Column="0" Width="35"
                                Style="{StaticResource ButtonStyle}" Command="{Binding Path=PlayButtonCommand}">
                            <Image Source="/SilverTrack;component/images/playpauseicon.png" Width="16" Height="16" />
                        </Button>
                        <ComboBox x:Name="PlaybackSpeedComboBox" Margin="3,0,0,0" Width="71"
                              Style="{StaticResource ComboBoxStyle}" BorderThickness="2" FontWeight="Bold" 
                              FontFamily="Arial" FontSize="10" IsDropDownOpen="True"
                              SelectedItem="{Binding Path=PlaybackSpeedSelectedItem, Mode=TwoWay}" SelectedIndex="3">
                            <ComboBoxItem Content="0.25" />
                            <ComboBoxItem Content="0.5" />
                            <ComboBoxItem Content="0.75" />
                            <ComboBoxItem Content="1" />
                            <ComboBoxItem Content="2" />
                            <ComboBoxItem Content="4" />
                            <ComboBoxItem Content="8" />
                        </ComboBox>
                        <Button x:Name="RestartButton" Width="35" Style="{StaticResource ButtonStyle}" Command="{Binding Path=RestartButtonCommand}">
                            <Image Source="/SilverTrack;component/images/restart.png" Width="16" Height="16"/>
                        </Button>
                        <Button x:Name="FullDataButton" Width="120" Style="{StaticResource ButtonStyle}" Command="{Binding Path=FullDataCommand}" Margin="3,0,5,0">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock VerticalAlignment="Center">SHOW FULL DATA</TextBlock>
                                <Image Source="/SilverTrack;component/images/end.png" Width="16" Height="16" />
                            </StackPanel>
                        </Button>
                    </StackPanel>
                    <!--Left Buttons Stack Panel-->
                    <StackPanel Orientation="Horizontal">
                        <Button x:Name="OpenFileButton" Width="110" Command="{Binding Path=OpenFileCommand}"
                            Style="{StaticResource ButtonStyle}" IsEnabled="{Binding Path=Paused}">
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                <Image Height="16" Source="/SilverTrack;component/images/playpauseicon.png" Width="16" />
                                <TextBlock VerticalAlignment="Center" Margin="4" >IMPORT DATA...</TextBlock >
                            </StackPanel>
                        </Button>
                        <Button Style="{StaticResource ButtonStyle}" Width="90" 
                            Command="{Binding Path=AddChartCommand}" IsEnabled="{Binding Path=Paused}">
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                <Image Height="16" Source="/SilverTrack;component/images/addchart.png" Width="16" />
                                <TextBlock VerticalAlignment="Center" Margin="4" >ADD CHART</TextBlock >
                            </StackPanel>
                        </Button>
                    </StackPanel>
                </Grid>
            </Border>

            <view:TelemetryChannelCollectionView DataContext="{Binding Path=ChannelViewModelCollection}" Grid.Row="2"/>

            <Grid Grid.Row="3">
                <Grid.RowDefinitions>
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>

                <Border Grid.Row="0" Background="{StaticResource YellowBackgroundImageBrush}">
                    <Button Style="{StaticResource ButtonStyle}" Height="15" Width="150" Click="Button_Click">TOGGLE WIDGETS</Button>
                </Border>
                <Border Grid.Row="1" x:Name="WidgetBorder" Height="160" Padding="0,5,0,0" Background="{StaticResource BackgroundImageBrush}">
                    <StackPanel Orientation="Horizontal">
                        <telemetry:ThrottleBrakeControl Brake="{Binding Path=Brake}" Throttle="{Binding Path=Throttle}" Margin="10,0"/>
                        <telemetryGauge:GaugeControl Value="{Binding Path=Speed}" Margin="10,0" Width="130" Maximum="250" Minimum="0" />
                        <telemetry:CarLocationControl x:Name="CarLocationControl" Position="{Binding Path=Position}" Margin="10,0"/>
                        <telemetry:GForceControl Lateral="{Binding Path=Lateral}" Long="{Binding Path=Long}" Margin="10,0"/>
                    </StackPanel>
                </Border>
            </Grid>

        </Grid>
    </Border>
</UserControl>

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