> | | > Dependency Properties - I (Dynamic XAML Binding)

Dependency Properties - I (Dynamic XAML Binding)

Posted on Wednesday, December 18, 2013 | No Comments

This is the first post in the a series of posts that would discuss various aspects of dependency properties and how they are different. The first aspect is XAML bind-ability of dependency properties.

For the purpose of demonstration, I have created a sample application in WPF, but this article applies to dependency properties in Silverlight/WinPhone/Windows8 Store applications as well.

Suppose the application contains a page that allows user to select products from a catalog and add them to the cart as shown below:


The set of controls in the Actions column in the above image has been created as a separate user control:


This user control is then embedded in the grid on the main page as:
 

            
                
                
                
                    
                        
                            
                        
                    
                

            
        

Now suppose we also want to specify the default Quantity for each product as 10 for all of these products since these are only sold in batches of 10. We would have to define a property in the user control, which the MainPage would be able to manipulate.

Let's define the property in the code-behind of the user control as a regular CLR property:

namespace DependencyProperties.UserControls
{
    namespace DependencyProperties.UserControls
{
    /// 
    /// Interaction logic for UpdateCart.xaml
    /// 
    public partial class UpdateCart : UserControl
    {
        public UpdateCart()
        {
            InitializeComponent();
        }

        private int _quantity = 0;

        public int Quantity
        {
            get
            {
                return _quantity;
            }
            set
            {
                udQuantity.Value = value;
            }
        }
    }
}

Here, udQuantity is the name of the up-down control inside this user control. udQuantity is, in turn, bound to the Quantity property in the view model of the user-control.
Now let's try binding this property in the XAML of the MainPage where this user control is embedded:

                    ;
                        
                            ;
                       
                    
               
This works. No problem. We have assigned a static value to the Quantity property of the user control. When the application is launched, it shows 10 instead of 0 in the Quantity up-down for all rows:

All good so far.

So why do we ever need dependency properties?

Now suppose this value 10 was not hard-coded but being passed in by some configuration file or some other calling application. Worse still, suppose this value was configured in the database and was different for each product type. How would be bind it then?

One dirty way could be to write code in the code-behind and capture the data grid's load/bind events and manipulate column values there, but that would cause performance issues if there is a lot of rows. Also, it may not always be easy to access the embedded control there and to set one of its properties.

This issue can easily be solved if we could bind the Quantity property of the user control to a property in the view model of the MainScreen. Something like:


                    
                        
                            
                        
                    
                
This is going to give you the following error:


So, in order to bind the property in XAML, you need to make it a dependency property as:

 public partial class UpdateCart : UserControl
    {
        public UpdateCart()
        {
            InitializeComponent();
        }
        public static readonly DependencyProperty QuantityProperty = DependencyProperty.Register("Quantity",
                                                                                    typeof(int),
                                                                                    typeof(UpdateCart),
                                                                                    new UIPropertyMetadata(QuantityPropertyChangedHandler));

        public static void QuantityPropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            // Get instance of current control from sender
            // and property value from e.NewValue

            // Set public property on TaregtCatalogControl, e.g.
            ((UpdateCart)sender).udQuantity.Value = (int)e.NewValue;
        }

        public int Quantity
        {
            get
            {
                return (int)GetValue(QuantityProperty);
            }
            set
            {
                SetValue(QuantityProperty, value);
            }
        }
    }


                    
                        
                            
                        
                    
                

This would allow you to perform dynamic binding on the property exposed by the user control.

Leave a Reply

Powered by Blogger.