Click here to Skip to main content
15,894,291 members
Articles / Desktop Programming / WPF

Introduction to Composite WPF (CAL, Prism): Part 2

Rate me:
Please Sign up or sign in to vote.
5.00/5 (16 votes)
19 Jul 2009CPOL17 min read 72.9K   1.5K   53  
An article showing an extremely simple implementation of CompositeWPF.
<ResourceDictionary 
    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/interactivedesigner/2006" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"
    >

    <!-- Simple ScrollRepeatButton Style - This RepeatButton is used above and below the Thumb in the Scrollbar. They are set to transparent si that they do not show over the scrollbar -->
    <Style x:Key="JamSoftScrollRepeatButtonStyle" d:IsControlPart="True" TargetType="{x:Type RepeatButton}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Grid>
                        <Rectangle StrokeThickness="{TemplateBinding BorderThickness}" 
                                   Fill="{DynamicResource MainAppColour}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    <Style TargetType="{x:Type ScrollBar}">
        <Setter Property="Stylus.IsFlicksEnabled" Value="false"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollBar}">
                    <Grid x:Name="GridRoot" 
                          Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" 
                          Background="{TemplateBinding Background}">
                        <Grid.RowDefinitions>
                            <RowDefinition MaxHeight="18"/>
                            <RowDefinition Height="0.00001*"/>
                            <RowDefinition MaxHeight="18"/>
                        </Grid.RowDefinitions>

                        <RepeatButton x:Name="DecreaseRepeat" 
                                      Style="{DynamicResource JamSoftRepeatButton}" 
                                      Command="ScrollBar.LineUpCommand" 
                                      Background="{DynamicResource AppBackground}">
                            <Grid>
                                <Path x:Name="DecreaseArrow" 
                                      Stroke="{TemplateBinding Foreground}" 
                                      StrokeThickness="1" 
                                      Data="M 0 4 L 8 4 L 4 0 Z" 
                                      Fill="{DynamicResource MainAppColour}"/>
                            </Grid>
                        </RepeatButton>

                        <!-- Track is a special layout container which sizes the thumb and the repeat button which do jump scrolling either side of it -->
                        <Track Grid.Row="1" x:Name="PART_Track" Orientation="Vertical" IsDirectionReversed="true">
                            <Track.Thumb>
                                <Thumb Style="{DynamicResource JamSoftThumbStyle}" 
                                       Background="{DynamicResource DefaultGradient}"/>
                            </Track.Thumb>
                            <Track.IncreaseRepeatButton>
                                <RepeatButton x:Name="PageUp" 
                                              Style="{DynamicResource JamSoftScrollRepeatButtonStyle}" 
                                              Command="ScrollBar.PageDownCommand"/>
                            </Track.IncreaseRepeatButton>
                            <Track.DecreaseRepeatButton>
                                <RepeatButton x:Name="PageDown" 
                                              Style="{DynamicResource JamSoftScrollRepeatButtonStyle}" 
                                              Command="ScrollBar.PageUpCommand"/>
                            </Track.DecreaseRepeatButton>
                        </Track>

                        <RepeatButton Grid.Row="2" 
                                      x:Name="IncreaseRepeat" 
                                      Style="{DynamicResource JamSoftRepeatButton}" 
                                      Command="ScrollBar.LineDownCommand" 
                                      Background="{DynamicResource AppBackground}">
                            <Grid>
                                <Path x:Name="IncreaseArrow" 
                                      Stroke="{TemplateBinding Foreground}" 
                                      StrokeThickness="1" 
                                      Data="M 0 0 L 4 4 L 8 0 Z" 
                                      Fill="{DynamicResource MainAppColour}"/>
                            </Grid>
                        </RepeatButton>
                    </Grid>

                    <!-- This uses a single template for ScrollBar and rotate it to be Horizontal
					It also changes the commands so that the it does Left and Right instead of Up and Down Commands -->
                    <ControlTemplate.Triggers>
                        <Trigger Property="Orientation" Value="Horizontal">

                            <!-- Rotate the ScrollBar from Vertical to Horizontal -->
                            <Setter Property="LayoutTransform" TargetName="GridRoot">
                                <Setter.Value>
                                    <RotateTransform Angle="-90"/>
                                </Setter.Value>
                            </Setter>

                            <!-- Track is bound to Orientation internally, so we need to rotate it back to Vertical -->
                            <Setter TargetName="PART_Track" Property="Orientation" Value="Vertical"/>

                            <!-- Change the commands to do Horizontal commands -->
                            <Setter Property="Command" Value="ScrollBar.LineLeftCommand" TargetName="DecreaseRepeat"/>
                            <Setter Property="Command" Value="ScrollBar.LineRightCommand" TargetName="IncreaseRepeat"/>
                            <Setter Property="Command" Value="ScrollBar.PageLeftCommand" TargetName="PageDown"/>
                            <Setter Property="Command" Value="ScrollBar.PageRightCommand" TargetName="PageUp"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="JamSoftRepeatButton" d:IsControlPart="True" TargetType="{x:Type RepeatButton}" BasedOn="{x:Null}">
        <Setter Property="Background" Value="{DynamicResource NormalBrush}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Grid>
                        <Border x:Name="Border" 
                                Background="{TemplateBinding Background}" 
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}"/>
                        <ContentPresenter HorizontalAlignment="Center" 
                                          x:Name="ContentPresenter" 
                                          VerticalAlignment="Center" 
                                          Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}" 
                                          ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="BorderBrush" Value="{DynamicResource DefaultedBorderBrush}" TargetName="Border"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background" Value="{DynamicResource MouseOverBrush}" TargetName="Border"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" Value="{DynamicResource PressedBrush}" TargetName="Border"/>
                            <Setter Property="BorderBrush" Value="{DynamicResource PressedBorderBrush}" TargetName="Border"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
                            <Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="Border"/>
                            <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="JamSoftThumbStyle" d:IsControlPart="True" TargetType="{x:Type Thumb}" BasedOn="{x:Null}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Grid Margin="0,0,0,0" x:Name="Grid">
                        <Rectangle HorizontalAlignment="Stretch" 
                                   x:Name="Rectangle" 
                                   VerticalAlignment="Stretch" 
                                   Width="Auto" 
                                   Height="Auto" 
                                   RadiusX="2" 
                                   RadiusY="2" 
                                   Fill="{DynamicResource DefaultGradient}" 
                                   Stroke="{DynamicResource AppBackground}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                        <Trigger Property="IsDragging" Value="True"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

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
Chief Technology Officer JamSoft Solution Ltd
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions