Home > 2013

2013

Dependency Properties -II (Memory Footprint)

Thursday, December 19, 2013 Category : , , 0



This is the second in a series of posts that would discuss various aspects of dependency properties and how they are different. The first aspect is memory footprint of the application in which they are defined.

Scenario:
A user control needs to be created which can be added to any page of the application and allows the user to add the selected item to the shopping cart after specifying the quantity and discount coupon code (optional). This user control would have UI similar to that shown below:

This control should expose a property Quantity, which can be set by the calling page. For example, a page can choose to show the user-control with Quantity pre-populated as 1:

Now suppose the user control’s class exposes this property as a regular CLR property:
public int? Quantity { get; set; }

Suppose this CLR property is exposed and this control is used by 10 products displayed on the screen. Each instance of those 10 instances of this user control would create a property called Quantity. So in the memory of the application, there would be 10 instances of user control - each with its own property Quantity.

If this CLR property is instead converted to a dependency property, there would be a single Quantity property in memory, which would be associated with each instance of the user control class. But how does a single instance of Quantity cater to the different values user may enter for each of those 10 instances? Answer: hashing.

As long as all 10 instances of the user control keep the default value of the property, they all will be sharing the same property in memory. However, if any one of these instances overrides the default value, a separate Quantity property would be spun in memory and would be linked to this particular instance using hashing.


This hashing mechanism is handled by the dependency engine. So as soon as a dependency property is created, a default value Quantity property is spun in memory. The hash is updated for each instance of the control to point to this Quantity. As soon as a control assigns a different value to the Quantity property of the user control, a separate memory location is assigned to that value and it is liked to the corresponding instance of user control using a hash. When that user control's Quantity property is accessed, the hash lookup is performed and the correct value is picked by the dependency engine.

Dependency Properties - I (Dynamic XAML Binding)

Wednesday, December 18, 2013 Category : , , 0

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.

Powered by Blogger.