WordPress database error: [INSERT, UPDATE command denied to user '51213-2'@'10.10.20.220' for table 'wp_options']INSERT INTO `wp_options` (`option_name`, `option_value`, `autoload`) VALUES ('_transient_doing_cron', '1715350943.9302449226379394531250', 'yes') ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)
<\/a>This is the part number 13 of the serie on the WPF 4.5 new features<\/a>.<\/p>\n ICustomTypeProvider is an interface already known in the Silverlight 5 world but it is shipped with full support in WPF 4.5 too. In this post we will discover what is the interface ICustomTypeProvider and how it can be used in WPF 4.5 !<\/p>\n <\/p>\n You may already know the ICustomTypeDescriptor (notice the ‘Descriptor<\/em>‘ instead of the ‘Provider<\/em>‘) interface as you are all WPF fans :-p ! This interface let you decide what information your type will expose when reflection is used. <\/strong><\/p>\n Binding uses reflection… So by implementing this interface you can define a set of dynamic properties and create binding targeting them<\/strong>. This is particulary useful when you are building a framework or when you know the properties name only at runtime.<\/p>\n ICustomTypeProvider is basically the same thing but it is lighter and designed for Silverlight at start.<\/strong><\/p>\n As this subject is covered since a long time for Silverlight 5, I won’t go in details on it. If you want more information you can read these helpful posts:<\/p>\n The thing to read in this post is that there is an helper class which helps you to create dynamic types<\/strong>. You can find an updated version of Alexandra’s one on Matthieu’s blog<\/a>. <\/p>\n The helper class does not works out of the box for WPF. Some pieces are actually missing for it to work \ud83d\ude42 but the fixes are not too hard to implement. Then you have to override the GetGetMethod and GetSetMethod of the CustomPropertyInfoHelper class<\/strong>. For this you’ll need the type o containing the property(I provide it trough the constructor). This can be done quick and dirty<\/em> like this: public override MethodInfo GetSetMethod(bool nonPublic) With the helper you can create types dynamically. In the demo project on my dropbox folder<\/a> (registration link<\/a>), I create a class named ‘MyNewClass’ and add a property ‘Name’ to it in a static constructor. public MyNewClass(string p) Once this is done, I can use it in a ViewModel to create a collection of this type: Then, I can create binding on the dynamic property Name in my XAML: <!– More xaml code –>
\n<\/strong><\/p>\nICustomTypeProvider : what is it ?<\/h3>\n
\n
Make it work for WPF 4.5<\/h3>\n
\nFirst of all, you must override the CustomType.TypeHandle property to return the correct TypeHandle<\/strong> :
\n[csharp]public override RuntimeTypeHandle TypeHandle
\n{
\n get { return _baseType.TypeHandle; }
\n}
\n[\/csharp]
\nIf you want to discover what is this struct RuntimeHandle and its purpose, read this post from Vance Morisson<\/a>.<\/p>\n
\n[csharp] public override MethodInfo GetGetMethod(bool nonPublic)
\n {
\n Action<object> getMethod = (object obj) =>
\n _fatherType.GetMethod("GetPropertyValue")
\n .Invoke(obj, new[] { _name });
\n return getMethod.Method;
\n }<\/p>\n
\n {
\n Action<object, object> setMethod = (object obj, object value) =>
\n _fatherType.GetMethod("SetPropertyValue")
\n .Invoke(obj, new[] { _name, value });
\n return setMethod.Method;
\n }
\n[\/csharp]<\/p>\nHow do I use it in my WPF app ?<\/h3>\n
\n[csharp] public class MyNewClass : CustomTypeHelper<MyNewClass>
\n {
\n static MyNewClass()
\n {
\n CustomTypeHelper<MyNewClass>.AddProperty("Name", typeof(string));
\n }<\/p>\n
\n {
\n SetPropertyValue("Name", p);
\n }
\n }
\n[\/csharp]<\/p>\n
\n[csharp] var classes = new ObservableCollection<MyNewClass>();
\n for (int i = 0; i < 50; i++)
\n {
\n classes.Add(new MyNewClass(string.Format("Person {0:##}", (1 + i))));
\n }
\n MyNewClasses = classes;
\n[\/csharp]<\/p>\n
\n[xml]<DataTemplate x:Key="PersonDataTemplate" DataType="model:Person">
\n <Border>
\n <TextBlock Text="{Binding Name}"\/>
\n <\/Border>
\n<\/DataTemplate><\/p>\n
\n <ListBox ItemsSource="{Binding MyNewClasses}"
\n ItemTemplate="{StaticResource PersonDataTemplate}"
\n HorizontalContentAlignment="Stretch" \/>
\n[\/xml]<\/p>\n