.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Silverlight UserControl Custom Property Binding

Posted By:Mahadesh Mahalingappa       Posted Date: August 08, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

In this article we would be seeing how to create a Cutom Silverlight UserControl and Bnding the Property . We would use dependency Property in this example
 


Silverlight UserControl Custom Property Binding :

In this article we would be seeing  how to create a Cutom Silverlight UserControl and Bnding the Property . We would use dependency Property in this example .

Created a Project as Sample Project .

Added an a new User Control as  MyNewControl.






Dependency Property :



Now lets add a dependency property . To know more about DP . Check out this site .

http://msdn.microsoft.com/en-us/library/cc221408(v=vs.95).aspx

My dependency property code looks as follows :

//  Dependency Property - Begin
        public const string DisplayTextPropertyName = "DisplayText";

        public string DisplayText
        {
            get
            {
                return (string)GetValue(DisplayTextProperty);
            }
            set
            {
                SetValue(DisplayTextProperty, value);
            }
        }
        
        public static readonly DependencyProperty DisplayTextProperty = DependencyProperty.Register(
DisplayTextPropertyName,typeof(string), typeof(MyNewControl),new PropertyMetadata(String.Empty));
        //  Dependency Property - End

The UserControl MyNewControl looks as below :

public partial class MyNewControl : UserControl
    {
//  Dependency Property - Begin
        public const string DisplayTextPropertyName = "DisplayText";

        public string DisplayText
        {
            get
            {
                return (string)GetValue(DisplayTextProperty);
            }
            set
            {
                SetValue(DisplayTextProperty, value);
            }
        }
        
        public static readonly DependencyProperty DisplayTextProperty = DependencyProperty.Register(
DisplayTextPropertyName,typeof(string), typeof(MyNewControl),new PropertyMetadata(String.Empty));
        //  Dependency Property - End






        public MyNewControl()
        {
            InitializeComponent();

            btn.SetBinding(Button.ContentProperty, new Binding() { Source = this, Path = new PropertyPath("DisplayText"), Mode = BindingMode.TwoWay });

        }
    }

MyNewControl is ready for use as shown below :



  
   
  


Lets give it  a run :




Great . Happy Coding !!
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend