Saturday, April 30, 2011

Several things that I always need in an application

Recently I have started on quite a few applications, and I found that reinventing the wheel has been a big part of the effort in them. They are all Data Binding applications in Silverlight/WPF. Several things that I found always needed in my applications:
1. Deep Clone: Definitely needed if you need a decent Undo/Redo capability in a Data Binding application.
2. Converters: Converters are a godsend in Silverlight/WPF application. If you are using Data Binding, which is the way to do a Silverlight/WPF app, then you have to use them. Out of the many converters that I did, these are my favorites:
a. Boolean To Visibility
b. Boolean To Brush
3. Implements INotifyPropertyChanged: This is also a given in a Data Binding application. One particular thing about my classes are that they may be from a Web Service (generated by SvcUtil) and you need to conform to the way SvcUtil choosen to implement the INotifyPropertyChanged.
4. Extend classes generated by the SvcUtil from Web Services: Mostly for direct use as the View Model. There are many traps and tricks when doing this. One of them is the inability to step through because of the stupid DebuggerStepThroughAttribute SvcUtil put on every class it generates.
5. Collection that is a union/combo of other collections: A view, so to speak, that merges contents from two or more ObservableCollections and show them as one. On top of that, I also need INotifyCollectionChanged events fired correctly on it when sub collections changed.
6. SetterValueBindingHelper: A great tool by Delay (David Anson) that solves the problem in Silverlight 2/3 (Can not set binding in Style setters).

Friday, April 22, 2011

Decent ways of converting a color from a string name to the Color class

//best, using Color's static method
Color red1 = Color.FromName("Red");
// using a ColorConverter
TypeConverter tc1 = TypeDescriptor.GetConverter(typeof(Color)); // ..or..
TypeConverter tc2 = new ColorConverter();
Color red2 = (Color)tc.ConvertFromString("Red");
// using Reflection on Color or Brush
Color red3 = (Color)typeof(Colors).GetProperty("Red").GetValue(null, null);
// in WPF you can use a BrushConverter
SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString("Red");

http://stackoverflow.com/questions/372693/convert-string-to-brushes-brush-name-in-c