Click here to Skip to main content
15,887,430 members
Articles / Programming Languages / C# 5.0
Tip/Trick

WPF Gradient Control

Rate me:
Please Sign up or sign in to vote.
4.94/5 (6 votes)
5 Jan 2015CPOL1 min read 24K   379   10   8
This is WPF Gradient Control. I didn't find a suitable gradient control which will generate a linear gradient or radial gradient brush. This is a gradient control which has a color selector and gradient generator. It can generate radial or linear gradient brush.

Introduction

There are few gradient controls on the net. I didn't find any control that will generate a gradient brush. This is a control which will generate a gradient brush from user selection. I found useful hints from Mark Betz (http://www.markbetz.net/sl/gradient/). It helped me a lot to develop this control. My web address is http://www.yagnaitsolution.ca.

Background

I am developing a reporting tool where a user can make reports from SQL or any data source. In the tools, I need something that will give the user to change the field background. So I was looking for a control which would help me to do this. But unfortunately, I didn't find anything that is perfect for me. So I decided to develop a Gradient brush control which will change the Report field background.

This control has couple of controls. One is color selector. That is, for a particular gradient stop, user can change the color. Color selector has a selector control which user can move and select the color. There is another control which is for gradient purposes. User can change the start point and end point for the Linear brush. For the Radial Brush, user can change center point, Origin point, RadiusX and RadiusY.

User can remove gradient stop or add gradient stop. It has a combobox from which user can select brush type.

Image 1

Image 2

Image 3

Using the Code

This is a user control, so there is no need to do anything. Just add as a user control to the window.

XML
<Style x:Key="{x:Type local:ColorSlider}" TargetType="{x:Type local:ColorSlider}">
        <Setter Property="Orientation" Value="Vertical"/>
        <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Foreground" 
        Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Minimum" Value="0"/>
        <Setter Property="Maximum" Value="100"/>
        <Setter Property="TickFrequency" Value="0.001" />
        <Setter Property="IsSnapToTickEnabled" Value="True" />
        <Setter Property="IsDirectionReversed" Value="False" />
        <Setter Property="Value" Value="1" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ColorSlider}">
                    <Border Background="{TemplateBinding Background}" 
            BorderBrush="{TemplateBinding BorderBrush}" 
            BorderThickness="{TemplateBinding BorderThickness}" 
            Height="{TemplateBinding Height}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"
                  MinWidth="{TemplateBinding Slider.MinWidth}"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <Border x:Name="PART_TrackBackground" Grid.Column="1" 
                Width="20" Margin="10,16,10,0">
                                <Rectangle x:Name="PART_SpectrumDisplay" 
                Stretch="Fill" VerticalAlignment="Stretch" />
                            </Border>
                            <Canvas Grid.Column="1" HorizontalAlignment="Center" Width="4">
                                <Rectangle x:Name="PART_SelectionRange" Canvas.Left="1" 
                Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" 
                Width="2.0" Visibility="Hidden" />
                            </Canvas>
                            <Track Grid.Column="1" Name="PART_Track">
                                <Track.DecreaseRepeatButton>
                                    <RepeatButton Style="{StaticResource SliderRepeatButtonStyle}"
                                Command="Slider.DecreaseLarge"/>
                                </Track.DecreaseRepeatButton>
                                <Track.IncreaseRepeatButton>
                                    <RepeatButton Style="{StaticResource SliderRepeatButtonStyle}" 
                    Command="Slider.IncreaseLarge"/>
                                </Track.IncreaseRepeatButton>
                                <Track.Thumb>
                                    <Thumb x:Name="Thumb" Height="16" 
                    Style="{DynamicResource ColorThumbStyle}" />
                                </Track.Thumb>
                            </Track>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <!-- Display SelectionRange background -->
                        <Trigger Property="IsSelectionRangeEnabled" Value="True">
                            <Setter TargetName="PART_SelectionRange"
                 Property="Visibility" Value="Visible"/>
                        </Trigger>
                        <Trigger Property="Width" Value="Auto">
                            <Setter Property="MinWidth" Value="21"/>
                        </Trigger>
                        <Trigger Property="Height" Value="Auto">
                            <Setter Property="MinHeight" Value="104"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

License

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


Written By
Web Developer Yagna IT Solution
Canada Canada
I have nothing to write. Im a senior programmer. I do work in c#, WPF, visual basic, oracle and developper, SQL Server, HTML5, CSS3 and JQuery.

Comments and Discussions

 
QuestionIt seems like a nice exercise: Pin
Rollin Shultz7-Jan-15 4:38
Rollin Shultz7-Jan-15 4:38 
AnswerRe: It seems like a nice exercise: Pin
Tanna7-Jan-15 6:40
professionalTanna7-Jan-15 6:40 
Questionvery nice Pin
BillW336-Jan-15 10:44
professionalBillW336-Jan-15 10:44 
Nicely written and well illustrated.
Just because the code works, it doesn't mean that it is good code.

AnswerRe: very nice Pin
Tanna7-Jan-15 6:42
professionalTanna7-Jan-15 6:42 
SuggestionAdd a demo project for download, and screens Pin
Emiliarge5-Jan-15 16:25
professionalEmiliarge5-Jan-15 16:25 
GeneralRe: Add a demo project for download, and screens Pin
Tanna5-Jan-15 16:46
professionalTanna5-Jan-15 16:46 
GeneralRe: Add a demo project for download, and screens Pin
Tanna5-Jan-15 16:51
professionalTanna5-Jan-15 16:51 
GeneralRe: Add a demo project for download, and screens Pin
Tanna5-Jan-15 20:38
professionalTanna5-Jan-15 20:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.