ARTICLE

Customize the Style of the Button And ToolTip Controls in Silverlight

Posted by Priya Linge Articles | Silverlight with C# July 26, 2011
This article demonstrates how to customize the style of the Button and ToolTip controls in Silverlight.
Reader Level:
 

Introduction:

This article demonstrates how to customize the style of the Button and ToolTip controls in Silverlight.

For customizing the style of a Button or ToolTip, we need to change its Template property.

With the help of the Template property we can change the existing styles of controls in Silverlight.

Those changed styles are customized styles of our controls.

Customize the Button Style in Silverlight

We can define a Button style as follows. With the help of ControlTemplate we can edit the existing style of the Button as follows. For design purpose we use path in the Button style.

<Style TargetType="Button" x:Key="btnstyle">
 <Setter Property="Background" Value="Black"/>
     <Setter Property="Template">
           <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <ContentPresenter>
                                <Path x:Name="BtnArrow" Height="12" Width="9" Stretch="Fill" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z " Margin="0,0,6,0" HorizontalAlignment="Right">
                                    <Path.Fill>
                                        <SolidColorBrush x:Name="BtnArrowColor" Color="Red"/>
                                    </Path.Fill>
                                    <Path.RenderTransform>
                                        <ScaleTransform x:Name="Scale" />
                                    </Path.RenderTransform>
                                </Path>
                            </ContentPresenter>
 
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
   </Style>

We can use this style in our Silverlight application with a Button control as follows.

  <Button Content="Button" Grid.Row="2" Height="23" HorizontalAlignment="Center" Name="button1" VerticalAlignment="Center" Width="25" Style="{StaticResource btnstyle}" Margin="209,51,167,17">

            <Button.RenderTransform>

                <RotateTransform Angle="180"></RotateTransform>

            </Button.RenderTransform>

    </Button>

This Button looks like as follows:

Customize the Button Style in Silverlight

ToolTip Style in Silverlight

We can define the ToolTip style as follows. With the help of ControlTemplate we can edit the existing style of the ToolTip as follows. For design purpose we use path in ToolTip style.

<Style x:Key="TooltipStyle" TargetType="ToolTip">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToolTip">
                        <Border BorderBrush="Red" BorderThickness="1">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>

                                </Grid.RowDefinitions>
                                <Path Fill="Black" Stretch="Fill"  Height="11" Width="10" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="6,0,0,0"
Stroke="#FF000000"  UseLayoutRounding="False" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "
                                    
 >
                                    <Path.RenderTransform>
                                        <RotateTransform Angle="180"></RotateTransform>
                                    </Path.RenderTransform>
                                </Path>
                                <ContentPresenter Grid.Row="0"
                                          Content="{TemplateBinding Content}" 
                                          HorizontalAlignment="Center" 
                                          VerticalAlignment
="Center"/>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

We can use this style in our Silverlight application with a ToolTip control as follows:

  <TextBox  Height="20" Width="100" Grid.Row="1" HorizontalAlignment="Center">
            <ToolTipService.ToolTip>
                <ToolTip Style="{StaticResource TooltipStyle}">
                    <TextBlock Text="Some text"/>
                </ToolTip>
 
            </ToolTipService.ToolTip>

   </TextBox>

This ToolTip looks like as follows:

ToolTip Style In Silverlight

We can also customize the ToolTip as follows. Here we use the above Button style.

                    <Button Content="Click me!" Width="100" Height="30" HorizontalAlignment="Center" Grid.Row="0" >
                        <ToolTipService.ToolTip>
                            <StackPanel>
                                <Button Height="18" Width="20" Style="{StaticResource btnstyle }" HorizontalAlignment="Left" Margin="6,4,0,0">
                                    <Button.RenderTransform>
                                        <RotateTransform Angle="180"></RotateTransform>
                                    </Button.RenderTransform>
                                </Button>
                            
                               
<TextBlock MaxWidth="150"
               Text="This is Text"
               FontFamily="Georgia" FontSize="14" TextWrapping
="Wrap"/>
                            </StackPanel>
 
                        </ToolTipService.ToolTip>
                    </Button>

The Customize ToolTip looks like as follows:

ToolTip Style In Silverlight

Summary:

We can change the existing styles of controls with the help of the Template property of the Silverlight controls. For customization of controls we use ControlTemplate.

Login to add your contents and source code to this article
post comment
     

Nice Article Thanks! for sharing it.

Posted by Vikrant More May 10, 2012

The Data property of path specifies the shape to be drawn. The Data attribute string begins with the "move to" command, indicated by M, which establishes a start point for the path in the coordinate system of the Canvas.End with Z Command. Path has Data. By specifying a value for Data you can supply the necessary visuals for any custom shape that can be described by a geometry, or even by the Path.Data .

Posted by Priya Linge Jul 28, 2011

About <ContentPresenter> Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z " How you get it from? And Please add me to be friends or brother or teacher. via msn : n123456789p@hotmail.com I'm Thai and are beginner programmer C# , my English so bad. Can you talking with me?

Posted by awesoms Desaw Jul 28, 2011

Very useful article, welcome

Posted by Dinesh Beniwal Jul 26, 2011

Nice Article....... Thanks for Sharing

Posted by Angelina Erin Jul 26, 2011
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts