SIGN UP MEMBER LOGIN:    
ARTICLE

WPF LinearGradientBrush using XAML

Posted by Purushottam Rathore Articles | WPF with C# February 25, 2009
This article and code snippet explains how to fill gradient color in the background of controls in WPF using XAML.
Reader Level:

Through this example I am going to explain how to fill gradient color in the background.

A LinearGradientBrush paints a gradient along a line. The line's start and end points are defined by the StartPoint and EndPoint properties of the LinearGradientBrush.

<LinearGradientBrush EndPoint="0.504,1.5" StartPoint="0.504,0.03">
</LinearGradientBrush>

StartPoint Property: Gets or sets the starting two-dimensional coordinates of the linear gradient. This is a dependency property.

EndPoint Property: Gets or sets the ending two-dimensional coordinates of the linear gradient. This is a dependency property.

GradientStop Class: Describes the location and color of a transition point in a gradient.

<GradientStop Color="#FFFFC934" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="0.567"/>

This example shows how to use the LinearGradientBrush class to paint an area with a linear gradient. In the following example, the Background of a Border is painted with linear gradient that transitions from yellow to white.

<Border.Background>
    <LinearGradientBrush EndPoint="0.504,1.5" StartPoint="0.504,0.03">
        <GradientStop Color="#FFFFC934" Offset="0"/>
        <GradientStop Color="#FFFFFFFF" Offset="0.567"/>
    </LinearGradientBrush>
</Border.Background>


gradientBackground.JPG

<
Window x:Class="Gradient.Window1" xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Title="Gradient" Height="300" Width="400">

<
Grid>
    <Border Margin="10,10,10,10" BorderBrush="Red" BorderThickness="1,1,1,1" CornerRadius="8,8,8,8">
        <Border.Background>
            <LinearGradientBrush EndPoint="0.504,1.5" StartPoint="0.504,0.03">
                <GradientStop Color="#FFFFC934" Offset="0"/>
                <GradientStop Color="#FFFFFFFF" Offset="0.567"/>
            </LinearGradientBrush>
        </Border.Background>

        <
DockPanel>
            <StackPanel Width="Auto" Height="235" DockPanel.Dock="Top" Orientation="Horizontal" VerticalAlignment="Top">

                <
StackPanel Margin="15,20,15,20">
                    <Border BorderBrush="White" BorderThickness="4" Width="108" Height="88">
                        <Image Source="cat.jpg" Height="80" Width="100" Stretch="Fill"></Image>
                    </Border>
                </StackPanel>

                <
StackPanel Margin="0,20,0,20">
                    <Label Content="Purushottam Rathore" FontSize="15" Foreground="Navy" />
                    <Label Content="India" FontSize="15" Foreground="Navy"/>
                </StackPanel>
            </StackPanel>
        </DockPanel>
    </Border>
</Grid>
</
Window>

gradient2.JPG

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor