Silverlight-Some Essential Dependency Properties

We often use custom control in silverlight and create custom property in controls. However, it occupies memory to create default value of property when creating a large amounts of custome controls. Actually, we can just use default value, not the property directly everytime. Therefore, DependencyProperty is introduced in silverlight and WPF to save memory and use property flexibly.

In silverlight, there are two methods to create property: CLR Custom Property, DependencyProperty and Attached Property.

1. CLR Custom Property Creation:

        private string _RectWidth;

        public string RectWidth

        {

            get { return _RectWidth; }

            set { _RectWidth = value; }

        }

2. DependencyPropery is saved one key value mathing dictionary which is based on DependencyObject class. It isn’t saved in class it belongs to so that we don’t need to crate a property default value everytime.

            #regionSet X Coordinate Property

            public double X

            {

                get { return (double)GetValue(XProperty); }

                set { SetValue(XProperty, value); }

            }

        //Create a DependencyProperty named X and set the location of rectangle1 to change when X changes.

        public static readonly DependencyProperty XProperty =

            DependencyProperty.Register(“X”, typeof(double), typeof(Rectan), new PropertyMetadata(OnXChanged));

        static void OnXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            Rectan rec = d as Rectan;

            rec.rectangle1.SetValue(Canvas.LeftProperty, e.NewValue);

        }

            #endregion

Please pay attention to the operation when DependencyProperty changing in OnXChanged(DependencyObject d, DependencyChangedEventArgs e). In this example, we change the location of rectangle1. After registering DependencyProperty, we can control it by XAML and CS.

XAML Controls DependencyProperty

            <uc:Rectan  X=“15” Y=“105” x:Name=“Uil” Width=“105” Height=“45”  HorizontalAlignment=“Left” VerticalAlignment=“Top”></uc:Rectan>

                CS Controls DependencyPropery

            this.Ikd.SetValue(Rectan.XProperty, 150.0);

            this.Ikd.SetValue(Rectan.YProperty, 150.0);

Then, let’s learn parameters and usage of DependencyProperty Register function.

It is to register DependencyProperty by using DependencyProperty.Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata);. We can do logic operation when property value changes in OnXChanged().

            // Summary

            // Use specified property name and type, owner type and dependency property which is registered by property metadata.

            // Parameter:

            // name:

            // Dependency Object name which is needed to register.

            // propertyType:

            // Property type.

            // ownerType:

            // Registering dependency object owner type.

            // typeMetadata:

            // Property Metadata. It can include System.Windows.PropertyChangedCallback to realize reference.

            // Return Result:

            // Use dependency object tag to set public static readonly value in your class. Then, you can use the tag to add dependency property as reference in your code or any other third party customers’¡¥ code. It can used in setting property value programmatically or append in code.

            // System.Windows.Data.Binding

            public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata);

3. Attached Property is one special DependencyPropery, which is global DependencyProperty. The method to register is DependencyProperty.RegisterAttached(); and other parameter same to  register DependencyProperty.

Complete XAML code and XAML.CS code:

Rectan.xaml.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace SLDependencyProp

{

    public partial class Rectan : UserControl

    {

        public Rectan()

        {

            InitializeComponent();

        }

        private string _RectWidth;

        public string RectWidth

        {

            get { return _RectWidth; }

            set { _RectWidth = value; }

        }

        #regionSet X Coordinate Property

        public double X

        {

            get { return (double)GetValue(XProperty); }

            set { SetValue(XProperty, value); }

        }

        // Create a DependencyProperty named X and set the location of rectangle1 to change when X changes.

        public static readonly DependencyProperty XProperty =

        DependencyProperty.Register(“X”, typeof(double), typeof(Rectan), new PropertyMetadata(OnXChanged));

        private static void OnXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            Rectan rec = d as Rectan;

            rec.rectangle1.SetValue(Canvas.LeftProperty, e.NewValue);

        }

        #endregion

        #regionSet Y Coordinate Property

        public double Y

        {

            get { return (double)GetValue(YProperty); }

            set { SetValue(YProperty, value); }

        }

        // Create a DependencyProperty named Y and set the location of rectangle1 to change when Y changes.

        public static readonly DependencyProperty YProperty =

        DependencyProperty.Register(“Y”, typeof(double), typeof(Rectan), new PropertyMetadata(OnYChanged));

        private static void OnYChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            Rectan rec = d as Rectan;

            rec.rectangle1.SetValue(Canvas.TopProperty, e.NewValue);

        }

        #endregion

    }

}

Rectan.xaml:

        <UserControl x:Class=“SLDependencyProp.Rectan”

        xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;

        xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml&#8221;

        xmlns:d=http://schemas.microsoft.com/expression/blend/2008&#8221;

        xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006&#8221;

        mc:Ignorable=“d”

        d:DesignHeight=“45” d:DesignWidth=“105”>

        <Canvas x:Name=“LayoutRoot” Background=“White”>

        <Rectangle Height=“45” HorizontalAlignment=“Left”  RadiusX=“15” RadiusY=“15” Fill=“BurlyWood”  Name=“rectangle1” Stroke=“Black” StrokeThickness=“1” VerticalAlignment=“Top” Width=“105” />

        </Canvas>

        </UserControl>

MainPage.xaml.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace SLDependencyProp

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

            this.Ikd.SetValue(Rectan.XProperty, 150.0);

            this.Ikd.SetValue(Rectan.YProperty, 150.0);

        }

    }

}

MainPage.xaml:

            <UserControl x:Class=“SLDependencyProp.MainPage”

            xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;

            xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml&#8221;

            xmlns:d=http://schemas.microsoft.com/expression/blend/2008&#8221;

            xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006&#8221;

            xmlns:uc=“clr-namespace:SLDependencyProp”

            mc:Ignorable=“d”

            d:DesignHeight=“300” d:DesignWidth=“400”>

            <Grid x:Name=“LayoutRoot” Background=“White”>

            <uc:Rectan  X=“15” Y=“105” x:Name=“Uil” Width=“105” Height=“45”  HorizontalAlignment=“Left” VerticalAlignment=“Top”></uc:Rectan>

            <uc:Rectan                x:Name=“Ikd”  Width=“105” Height=“45” HorizontalAlignment=“Left” VerticalAlignment=“Top”></uc:Rectan>

            <uc:Rectan   Width=“105” Height=“45” HorizontalAlignment=“Left” VerticalAlignment=“Top”></uc:Rectan>

            </Grid>

            </UserControl>

Two Components Recommend:

Spire.Doc, is used to operate MS Word document for .Net and Silverlight, including basic manipulations (generate, open, edit documents), mail merge and other Word functions manipulations.

Spire.XLS, is used to operate MS Excel for .NET and Silverlight, including basic manipulations (generate, open, edit files), chart creation and data exporting.

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s