Suggestion: New feature:-> Support for adding custom properties to any View-Model classes at runtime


Hi Markus,

I am suggesting this feature because i think this can be very useful for many developers. Recently we had a requirement to add properties to view-model classes at run-time and those properties should be bindable to the view-model controls.

For this we had to modify the ViewModel class of Code.framework. Changes are as follows:

(1)Inherited ViewModel class from 'CustomTypeDescriptor' (exists in System.ComponentModel namespace)
(2)Added new class 'CustomPropertyDescriptor' to the project which extends the PropertyDescriptor class
(3)Added a private list _myProperties of List<PropertyDescriptor> type (this too belongs to above namespace) to hold custom properties
(4)Overridden the 'GetProperties()' method to return return real properties along with custom properties (WPF data-binding calls this method)
(5)Added public methods 'AddProperty', 'GetPropertyValue', 'SetPropertyValue' etc. for access custom properties
(6)Also added 'CustomProperties' property to expose collection of custom properties & a method 'RemoveAllCustomProperties' to clear all custom properties.

I have attached modified 'ViewModel.cs' class (which is based on the latest version 4.0.41021.0) and a new class 'CustomPropertyDescriptor.cs'

Please let me know if any additional information is required regarding these changes/feature.

I hope you find these changes useful & will be delighted to see them included in the next version of Code.framework!

Thanks & Regards,

file attachments

Closed Dec 4, 2016 at 5:47 AM by MarkusEgger


rahulpatil2009 wrote Nov 24, 2014 at 1:42 PM

Please ignore first attachment 'ViewModel.cs'. All changes are in 'CFChanges.zip' file.

MarkusEgger wrote Nov 25, 2014 at 7:52 AM


I see where you are going with this, but you can already do that, can't you? Basically, what you are describing is a dynamic type, which you can do in two ways, currently. You can add a dictionary to a regular type/class and then bind to members in the dictionary. WPF supports that. (This would often be referred to as a "state bag" or "name/value pairs"). I think that is the approach I would use for what you describe.

The other approach is to use an actual C# dynamic object. There are various ways of doing that. What you describe above is one such approach, but there are others. Doing that is useful in many scenarios. It also has downsides though. One is that performance suffers. The other is that there are behavioral and computational side-effects (such as whether the class remains transferable to other environments). In other words: If I just rolled that out as a general CODE Framework feature, I would break some people's code and influence the performance of others.

However, keep in mind that as everything in CODE Framework, what we provide out of the box is meant as a useful starting point, but not the only option. There is no requirement in the framework to use our view model class. You can use anything you want as a view-model. So it is perfectly fine for you to create a new class, call it something like "DynamicViewModel", and then derive your view models from that rather than the original one.


wrote Nov 25, 2014 at 7:52 AM

wrote Dec 4, 2016 at 5:47 AM