El maravilloso universo de la ingenería y desarrollo de software


Como generar un botón reutilizable con Microsoft Expresion Blend 2 para Silverlight 2

Jun-132008

Una de las facilidades que ofrece Microsoft Expression Blend es la capacidad de integrar elementos de diseño fácilmente y de la mano con Visual Studio 2008 extender la funcionalidad de los elementos gráficos.

Veremos cómo podemos diseñar un control y agregar propiedades que permitan a nuestro control modificar su diseño en base a propiedades comunes.

El día de hoy generaremos un botón.

Inicializamos un nuevo proyecto con Microsoft Expression Blend de tipo Silverlight 2 Application

clip_image002

Una vez generada la solución agregamos un UserControl el cual llamaremos CustomButton

clip_image004

En nuestro CustomControl agregamos el siguiente código Xaml para darle un estilo a nuestro botón.

 

<UserControl xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="SampleApp.CustomButton" Width="Auto" Height="Auto"> <Grid Height="Auto" Width="Auto" x:Name="canvas" RenderTransformOrigin="0.5,0.5"> <Grid.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </Grid.RenderTransform> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.346*"/> <ColumnDefinition Width="0.256*"/> <ColumnDefinition Width="0.398*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="0.287*"/> <RowDefinition Height="0.235*"/> <RowDefinition Height="0.309*"/> <RowDefinition Height="0.169*"/> </Grid.RowDefinitions> <Rectangle Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

Width="Auto" Fill="#FF0D3A66" Stroke="#FF000000" RadiusX="8" RadiusY="8" d:LayoutOverrides="HorizontalAlignment" Grid.RowSpan="4" Grid.ColumnSpan="3" x:Name="border"/> <Path Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

Data="M71.25,29.125 C73.858894,29.351784 2.729677,28.617605 0,29.125 C0,13.039706 15.949856,0 35.625,0 C55.300144,0 71.25,13.039706 71.25,29.125 z"

Stretch="Fill" Grid.RowSpan="1" Grid.Row="3" Grid.ColumnSpan="1" d:LayoutOverrides="HorizontalAlignment" Grid.Column="1" Margin="0,0,0,1" x:Name="borderdown" RenderTransformOrigin="0.5,0.5"> <Path.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </Path.RenderTransform> <Path.Fill> <RadialGradientBrush GradientOrigin="0.495999991893768,-0.184000000357628"> <RadialGradientBrush.RelativeTransform> <TransformGroup> <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="-1.041" ScaleY="-1.989"/> <SkewTransform CenterX="0.5" CenterY="0.5"/> <RotateTransform CenterX="0.5" CenterY="0.5"/> <TranslateTransform Y="0.474"/> </TransformGroup> </RadialGradientBrush.RelativeTransform> <GradientStop Color="#FF0D3A66" Offset="1"/> <GradientStop Color="#7FFFFFFF" Offset="0"/> </RadialGradientBrush> </Path.Fill> </Path> <Path HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

Data="M165.54631,113.71158 C165.54646,138.17401 129.82472,136.5 83.8125,136.5 C37.800278,136.5 2.568521,136.25545 2.1868401,113.71167 C2.2377911,113.72384 1.6872363,93.100006 2.1868401,93.110977 C13.487612,93.359039 140.10013,93.274727 165.27357,93.110977 C168.31805,93.091171 165.68532,113.73421 165.54631,113.71158 z"

Stretch="Fill" d:LayoutOverrides="HorizontalAlignment" Grid.RowSpan="2" Grid.ColumnSpan="3" Margin="2,5,2,0" x:Name="borderup"> <Path.Fill> <LinearGradientBrush EndPoint="0.51800000667572,0.994000017642975" StartPoint="0.512000024318695,-0.0670000016689301"> <GradientStop Color="#FF0D3A66" Offset="0.009"/> <GradientStop Color="#4CFFFFFF" Offset="1"/> </LinearGradientBrush> </Path.Fill> </Path> <TextBlock x:Name="text" Grid.ColumnSpan="3" Grid.Row="1" FontSize="14" Text="Texto" TextWrapping="Wrap"

Foreground="#FFFFFFFF" FontWeight="Bold" HorizontalAlignment="Center" d:LayoutOverrides="GridBox" Grid.Column="0" Grid.RowSpan="2"/> </Grid> </UserControl>

 

Nuestro botón quedaria de la siguiente manera:

image

Enseguida agregamos el código que permitira que nuestro control muestre las propiedades que nosotros deseamos extender.

A nuestro control agregaremos un estilo de letra y color de fondo por default.

public CustomButton()
        {
            InitializeComponent();
            FontSize = 14;
            ColorButton = Color.FromArgb(255, 13, 58, 102);
        }

Si deseamos que el usuario personalice por ejemplo el tamaño de la fuente del texto es necesario exponer una propiedad que permita manipular el tamaño de la fuente.

Veamos como generamos una propiedad para manipular el tamaño de la fuente de nuestro control.

Para este escenario es necesario agregar una propiedad pública, pero esta propiedad no es de un tipo especial llamado DependencyProperty el cual permite mantener el valor de una propiedad aún y cuando se serialice el objeto y además es si queremos que la propiedad sea visible en el Expression Blend, es necesario su utilización.

        // Using a DependencyProperty as the backing store for FontSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FontSizeProperty =
            DependencyProperty.Register("FontSize", typeof(double), typeof(CustomButton), new PropertyMetadata(FontSizeChangedCallback));

Agregamos un propiedad tradicional pero con la diferencia que el valor es asignado a la propiedad de dependencia que acabamos de crear.

        public double FontSize
        {
            get { return (double)GetValue(FontSizeProperty); }
            set { SetValue(FontSizeProperty, value); }
        }

Finalmente agregamos un evento para que en el momento que el valor de nuestra propiedad cambie, asignemos el valor a nuestro control de texto, este texto debido a que se encuentra en el contexto de nuestro control es privado y no puede ser accedido si lo incluimos en otro control, página o contenedor.

        private static void FontSizeChangedCallback(DependencyObject obj,
                        DependencyPropertyChangedEventArgs args)
        {
            //Asigna el texto
            CustomButton button = obj as CustomButton;
            if (button != null)
            {
                button.text.FontSize = (double)args.NewValue;
            }
        }

Esta propiedad será visible en el diseñador de Expression.

Para incluir el control dentro de otra página o control de expression, simplemente es necesario buscarlo en la sección de Asset Library, en la sección  de Custom Controls, ahí ya aparecerá nuestra clase de CustomButton.

image

Arrastramos nuestro control y podremos ver el mismo diseño pero sin la posibilidad de modificar su estilo, pero si podremos modificar la nueva propiedad que acabamos de generar.

Nota: para que cualquier cambio hecho a nuestro control se vea reflejado, es necesario compilar la solución.

image

 

Podemos agregar más propiedades tales como configurar el color de fondo y el texto a mostrar.

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(CustomButton), new PropertyMetadata(TextChangedCallback));

        private static void TextChangedCallback(DependencyObject obj,
                        DependencyPropertyChangedEventArgs args)
        {
            //Asigna el texto
            CustomButton button = obj as CustomButton;
            if (button != null)
            {
                button.text.Text = args.NewValue as string;
            }
        }



        public Color ColorButton
        {
            get { return (Color)GetValue(ColorButtonProperty); }
            set { SetValue(ColorButtonProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Color.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ColorButtonProperty =
            DependencyProperty.Register("ColorButton", typeof(Color), typeof(CustomButton), new PropertyMetadata(ColorButtonChangedCallback));

        private static void ColorButtonChangedCallback(DependencyObject obj,
                        DependencyPropertyChangedEventArgs args)
        {
            //Asigna el texto
            CustomButton button = obj as CustomButton;
            if (button != null)
            {
                if (button.border.Fill is SolidColorBrush)
                {
                    SolidColorBrush solid = button.border.Fill as SolidColorBrush;
                    solid.Color = button.ColorButton;
                }
                if (button.borderdown.Fill is RadialGradientBrush)
                {
                    RadialGradientBrush radial = button.borderdown.Fill as RadialGradientBrush;
                    radial.GradientStops[0].Color = button.ColorButton;
                }
                if (button.borderup.Fill is LinearGradientBrush)
                {
                    LinearGradientBrush linear = button.borderup.Fill as LinearGradientBrush;
                    linear.GradientStops[0].Color = button.ColorButton;
                }
            }
        }

Con estas propiedades podemos personalizar nuestros botones.

 

image

Nos faltaría como agregar o extender eventos, pero ese tema lo tratare en otro post.

¿Y el código?

El código fuente de este control esta disponible aquí.

 
Posted by Efren Esteban Cruz Anguiano | 0 Comments | Trackback Url | Bookmark with:        
Tags: Design

Links to this Post

Hydromorphone overnight delivery
Trackback from Hydromorphone overnight delivery on 14 Nov 2008 05:34


Purchase acepromazine
Trackback from Purchase acepromazine on 14 Nov 2008 05:40


Buy wholesale tramadol 100mg
Trackback from Buy wholesale tramadol 100mg on 14 Nov 2008 05:46


200 mg hexal tramadol
Trackback from 200 mg hexal tramadol on 14 Nov 2008 05:52


Hydromorphone hcl pharmacy no prescription
Trackback from Hydromorphone hcl pharmacy no prescription on 14 Nov 2008 05:53


Tramadol withdrawal in dogs
Trackback from Tramadol withdrawal in dogs on 14 Nov 2008 05:55


Take tramadol with advil
Trackback from Take tramadol with advil on 14 Nov 2008 05:56


Buying acepromazine
Trackback from Buying acepromazine on 14 Nov 2008 05:57


Tramadol hcl 50 mg - pain killer
Trackback from Tramadol hcl 50 mg - pain killer on 14 Nov 2008 05:58


Order ultram shipped to florida
Trackback from Order ultram shipped to florida on 14 Nov 2008 05:59


Indian pills
Trackback from Indian pills on 14 Nov 2008 06:00


Dilaudid online without prescription
Trackback from Dilaudid online without prescription on 14 Nov 2008 06:01


Advil wholesale price 50 tablets
Trackback from Advil wholesale price 50 tablets on 14 Nov 2008 06:03


Tramadol mexico over the counter
Trackback from Tramadol mexico over the counter on 14 Nov 2008 06:03


Hydromorphone rx
Trackback from Hydromorphone rx on 14 Nov 2008 06:04


What class of medicine is acepromazine?
Trackback from What class of medicine is acepromazine? on 14 Nov 2008 06:05


Tramadol addiction potential
Trackback from Tramadol addiction potential on 14 Nov 2008 06:06


Ultram vs tramadol
Trackback from Ultram vs tramadol on 14 Nov 2008 06:07


Very cheap tramadol
Trackback from Very cheap tramadol on 14 Nov 2008 06:09


325 mg tramadol cheap
Trackback from 325 mg tramadol cheap on 14 Nov 2008 06:10


Tramadol germany
Trackback from Tramadol germany on 14 Nov 2008 06:11


Buy tramadol ups
Trackback from Buy tramadol ups on 14 Nov 2008 06:12


Acepromazine for purchase
Trackback from Acepromazine for purchase on 14 Nov 2008 06:13


Tramadol maximum dose
Trackback from Tramadol maximum dose on 14 Nov 2008 06:14


Tramadol 200mg buy online
Trackback from Tramadol 200mg buy online on 14 Nov 2008 06:15


Tablets order india
Trackback from Tablets order india on 14 Nov 2008 06:16


How to abuse dilaudid
Trackback from How to abuse dilaudid on 14 Nov 2008 06:17


Pain pills online
Trackback from Pain pills online on 14 Nov 2008 06:18


200 mg tramadol overnight delivery, fedex
Trackback from 200 mg tramadol overnight delivery, fedex on 14 Nov 2008 06:19


Overnight tramadol no prescription
Trackback from Overnight tramadol no prescription on 14 Nov 2008 06:20


Appearance of painkillers
Trackback from Appearance of painkillers on 14 Nov 2008 06:24


To buy opioids
Trackback from To buy opioids on 14 Nov 2008 06:31


Cheapest tramadol available online
Trackback from Cheapest tramadol available online on 14 Nov 2008 06:37


Snorting tramadol effects
Trackback from Snorting tramadol effects on 14 Nov 2008 06:42


Dilaudid no prescription mexico
Trackback from Dilaudid no prescription mexico on 14 Nov 2008 06:44


Maximum dose tramadol hydrochloride
Trackback from Maximum dose tramadol hydrochloride on 14 Nov 2008 06:45


Overdose of tramadol in dog
Trackback from Overdose of tramadol in dog on 14 Nov 2008 06:46


Does tramadol from india work
Trackback from Does tramadol from india work on 14 Nov 2008 06:47


Canine rx acepromazine
Trackback from Canine rx acepromazine on 14 Nov 2008 06:48


Tramadol pills
Trackback from Tramadol pills on 14 Nov 2008 06:50


Tramadol pills and american express
Trackback from Tramadol pills and american express on 14 Nov 2008 06:51


Tramadol taken with advill
Trackback from Tramadol taken with advill on 14 Nov 2008 06:52


Crushing tramadol and snorting it
Trackback from Crushing tramadol and snorting it on 14 Nov 2008 06:53


Tramadol use in dogs
Trackback from Tramadol use in dogs on 14 Nov 2008 06:54


Taking tylonol 2 with tramadol
Trackback from Taking tylonol 2 with tramadol on 14 Nov 2008 06:55


Dangers of tramadol usage
Trackback from Dangers of tramadol usage on 14 Nov 2008 06:56


India tramadol
Trackback from India tramadol on 14 Nov 2008 06:57


Cost of tramadol tablets
Trackback from Cost of tramadol tablets on 14 Nov 2008 06:58


Risk tramadol snorting
Trackback from Risk tramadol snorting on 14 Nov 2008 07:00


Tramadol without prescription in foreign
Trackback from Tramadol without prescription in foreign on 14 Nov 2008 07:01


Canine tramadol dose
Trackback from Canine tramadol dose on 14 Nov 2008 07:03


Tramadol hcl user reviews
Trackback from Tramadol hcl user reviews on 14 Nov 2008 07:04


Snorting tramadol hydrochloride
Trackback from Snorting tramadol hydrochloride on 14 Nov 2008 07:05


Tramadol india
Trackback from Tramadol india on 14 Nov 2008 07:06


Buy tramadol on line
Trackback from Buy tramadol on line on 14 Nov 2008 07:07


Tramadol delivery to florida
Trackback from Tramadol delivery to florida on 14 Nov 2008 07:08


Advil with tramadol
Trackback from Advil with tramadol on 14 Nov 2008 07:09


Order cheap pain medicine online with cod delivery
Trackback from Order cheap pain medicine online with cod delivery on 14 Nov 2008 07:10


Side effects of tramadol uses in canines
Trackback from Side effects of tramadol uses in canines on 14 Nov 2008 07:11


Tramadol 50 mg mexico
Trackback from Tramadol 50 mg mexico on 14 Nov 2008 07:12


Tramadol 100 mg without a prescription
Trackback from Tramadol 100 mg without a prescription on 14 Nov 2008 07:13


Can you take tramadol with soma
Trackback from Can you take tramadol with soma on 14 Nov 2008 07:20


200 tramadol 50mg order
Trackback from 200 tramadol 50mg order on 14 Nov 2008 07:25


Tramadol induced headaches
Trackback from Tramadol induced headaches on 14 Nov 2008 07:31


Tramadol 50 mg canines
Trackback from Tramadol 50 mg canines on 14 Nov 2008 07:32


Tramadol hci headaches
Trackback from Tramadol hci headaches on 14 Nov 2008 07:33


Side effects of tramadol hydrochloride in dogs
Trackback from Side effects of tramadol hydrochloride in dogs on 14 Nov 2008 07:35


Immediate effect of tramadol pill
Trackback from Immediate effect of tramadol pill on 14 Nov 2008 07:37


Tramadol fedex overnight
Trackback from Tramadol fedex overnight on 14 Nov 2008 07:38


Dilaudid online injection
Trackback from Dilaudid online injection on 14 Nov 2008 07:40


Acepromazine where to buy
Trackback from Acepromazine where to buy on 14 Nov 2008 07:42


Buy cheap tramadol tramadol mg quick delivery cod
Trackback from Buy cheap tramadol tramadol mg quick delivery cod on 14 Nov 2008 07:43


Buy dilaudid
Trackback from Buy dilaudid on 14 Nov 2008 07:45


Low price acepromazine
Trackback from Low price acepromazine on 14 Nov 2008 07:46


Tramadol buy germany
Trackback from Tramadol buy germany on 14 Nov 2008 07:47


Mex. drugs rx farmacia express
Trackback from Mex. drugs rx farmacia express on 14 Nov 2008 07:48


Tramadol buy with amrerican express
Trackback from Tramadol buy with amrerican express on 14 Nov 2008 07:50


Buy dilaudid free prescription
Trackback from Buy dilaudid free prescription on 14 Nov 2008 07:51


Side effects of tramadol hci in dogs
Trackback from Side effects of tramadol hci in dogs on 14 Nov 2008 07:52


Presciption pain medication
Trackback from Presciption pain medication on 14 Nov 2008 07:53


Tramadol no prescription
Trackback from Tramadol no prescription on 14 Nov 2008 07:55


Ultram without a prescription
Trackback from Ultram without a prescription on 14 Nov 2008 07:55


Tramadol overdose treatment
Trackback from Tramadol overdose treatment on 14 Nov 2008 07:57


Where to get dilaudid
Trackback from Where to get dilaudid on 14 Nov 2008 07:58


Discount tramadol for dogs
Trackback from Discount tramadol for dogs on 14 Nov 2008 07:59


Tramadol pain med for dogs
Trackback from Tramadol pain med for dogs on 14 Nov 2008 08:00


Prescriptions from mexico
Trackback from Prescriptions from mexico on 14 Nov 2008 08:01


Enhancing tramadol
Trackback from Enhancing tramadol on 14 Nov 2008 08:08


Ship to florida tramadol
Trackback from Ship to florida tramadol on 14 Nov 2008 08:13


Tramadol fedex overnight no prescription
Trackback from Tramadol fedex overnight no prescription on 14 Nov 2008 08:19


Is tramadol a good pain pill
Trackback from Is tramadol a good pain pill on 14 Nov 2008 08:21


Super cheap tramadol by fedex
Trackback from Super cheap tramadol by fedex on 14 Nov 2008 08:22


Can you crush tramadol
Trackback from Can you crush tramadol on 14 Nov 2008 08:23


Tramadol by echeck
Trackback from Tramadol by echeck on 14 Nov 2008 08:25


Dilaudid order
Trackback from Dilaudid order on 14 Nov 2008 08:26


How to purchase dilaudid
Trackback from How to purchase dilaudid on 14 Nov 2008 08:27


Buy dilaudid hydromorphone
Trackback from Buy dilaudid hydromorphone on 14 Nov 2008 08:28


Snort tramadol hydrochloride
Trackback from Snort tramadol hydrochloride on 14 Nov 2008 08:29


Buy dilaudid without a prescription
Trackback from Buy dilaudid without a prescription on 14 Nov 2008 08:31


Tramadol pain med
Trackback from Tramadol pain med on 14 Nov 2008 08:31


Tramadol delivery florida
Trackback from Tramadol delivery florida on 14 Nov 2008 08:33


Order rx from india
Trackback from Order rx from india on 14 Nov 2008 08:34


Buying dilaudid online with no percription
Trackback from Buying dilaudid online with no percription on 14 Nov 2008 08:35


Need to order tramadol live in florida
Trackback from Need to order tramadol live in florida on 14 Nov 2008 08:36


100 mg tramadol
Trackback from 100 mg tramadol on 14 Nov 2008 08:38


Tramadol hydrochloride taken with prozac
Trackback from Tramadol hydrochloride taken with prozac on 14 Nov 2008 08:39


Canine tramadol chills
Trackback from Canine tramadol chills on 14 Nov 2008 08:40


Intravenous tramadol canine
Trackback from Intravenous tramadol canine on 14 Nov 2008 08:41


100mg tramadol overnight
Trackback from 100mg tramadol overnight on 14 Nov 2008 08:42


Tramadol in liquid
Trackback from Tramadol in liquid on 14 Nov 2008 08:43


Dilaudid order online pharmacy
Trackback from Dilaudid order online pharmacy on 14 Nov 2008 08:44


Hydromorphone overnight delivery
Trackback from Hydromorphone overnight delivery on 14 Nov 2008 08:45