Click here to Skip to main content
15,896,259 members
Articles / Desktop Programming / WPF

WPF Custom Controls - Without The Pain

Rate me:
Please Sign up or sign in to vote.
4.54/5 (36 votes)
13 Aug 2008CPOL4 min read 198.4K   7.5K   157  
Creating a maintainable, extensible WPF custom control library is a lot easier if you know a few tricks.
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomControls">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/CustomControls;component/Themes/resXSButtonStyles.xaml" />
    </ResourceDictionary.MergedDictionaries>


    <Style TargetType="{x:Type local:XSButton}" >
        
        <!-- Provide DEFAULT values for some properties -->
        <Setter Property="CornerRadius" Value="4" />
        <Setter Property="Background" Value="{StaticResource brushClearGlass}" />
        <Setter Property="OuterBorderBrush" Value="{StaticResource brushOuterBorder}" />
        <Setter Property="OuterBorderThickness" Value="1" />
        <Setter Property="InnerBorderBrush" Value="{StaticResource brushInnerBorder}" />
        <Setter Property="InnerBorderThickness" Value="1" />
        <Setter Property="GlowColor" Value="{StaticResource brushGlow}" />
        
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:XSButton}">

                    <Border Name="OuterBorder" CornerRadius="{Binding CornerRadius, ElementName=InnerBorder}"
                            BorderBrush="{TemplateBinding OuterBorderBrush}" BorderThickness="{TemplateBinding OuterBorderThickness}" >
                        
                        <Border Name="InnerBorder" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding CornerRadius}"
                                BorderBrush="{TemplateBinding InnerBorderBrush}" BorderThickness="{TemplateBinding InnerBorderThickness}" >
                            
                            <Grid >

                                <Border Name="Glow" Opacity="0" CornerRadius="{Binding CornerRadius, ElementName=InnerBorder}">
                                    <Border.Background>
                                        <RadialGradientBrush>
                                            <GradientStop Offset="0" Color="{Binding GlowColor, RelativeSource={RelativeSource TemplatedParent},
                                                Converter={StaticResource ColorToAlphaColorConverter}, ConverterParameter=176}" />
                                            <GradientStop Offset="1" Color="{Binding GlowColor, RelativeSource={RelativeSource TemplatedParent},
                                                Converter={StaticResource ColorToAlphaColorConverter}, ConverterParameter=0}" />
                                        </RadialGradientBrush>
                                    </Border.Background>
                                </Border>
                                
                                <Border Name="padding" Margin="{TemplateBinding Padding}"
                                        HorizontalAlignment="Center" VerticalAlignment="Center">
                                    <ContentPresenter Name="content" />
                                </Border>
                                
                                <Control Name="Highlight" Template="{TemplateBinding HighlightAppearance}" />
                                
                            </Grid>
                        </Border>
                    </Border>
                    
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="OuterBorder" Property="Opacity" Value="0.9" />
                            <Setter TargetName="InnerBorder" Property="Opacity" Value="0.9"/>
                            <Setter TargetName="content" Property="Margin" Value="2,2,0,0"/>
                            <Setter TargetName="Glow" Property="Opacity" Value="0.5" />
                            <Setter TargetName="Highlight" Property="Opacity" Value="0.5"/>
                        </Trigger>
                        
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource GlowOn}"/>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource GlowOff}"/>
                            </Trigger.ExitActions>
                        </Trigger>
                    </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
United States United States
I started out writing applications in C for a software development shop in Japan, did alot of work in C++/MFC, and some DirectX, at two Silicon Valley startups, and have been working with C# and Windows Forms ever since the release of .Net 1.0. Although I took a couple intro. to CS courses at CAL (Berkeley), my degree was actually in Asian Studies, and I learned to program "in the trenches". I was also the .Net evangelist at my most recent company, authoring internal white papers on .Net, sending out a weekly ".Net FYI" e-mail, conducting .Net training, and sweating the truth out of job candidates who claimed to have .Net experience (You'd be amazed at the number of Silicon Valley engineers who list "three years of C#" on their resumes, who are unable to explain how to hook up a simple event handler, or identify basic terms like "reflection", "attributes" -- or "Richter" and "Löwy").

Comments and Discussions