BiDi Applications With Silverlight 4
I started checking out the new Silverlight 4 Beta this week and the most important feature (for me) added to this release : FlowDirection. If you’re not a WPF guy, this is the equivalent of RightToLeft in WinForm world, just recently added to Silverlight. If this works the way the same feature in WPF works, all custom controls built in this platform will automagically have correct RightToLeft behavior, “Almost” without any additional code from component vendor / developer. Sounds too good to be true, but it is.
To test how this works, we need a Silverlight 4 control first. Well, look no further! AgDataGrid from DevExpress is both free and open-source! Register and grab your version from here. Should you need to see some demos of this control’s features go here.
The build is for latest released version of Silverlight 3, so you need to compile it against Silverlight 4 runtime. Open the solution and change the framework version to SL 4 and build it. That’s it.
Now a Silverlight Application and remember to select “Silverlight 4” runtime when Visual Studio asks which runtime version to use. Add reference to assemblies you just built and add a agDataGrid instance to your page:
<dg:AgDataGrid x:Name="grid" FlowDirection="RightToLeft"
The new FlowDirection property on AgDataGrid is added automatically just by compiling it against SL 4.
To make it a better example, let’s add some more sugar to the pot. To localize the GroupPanel text, let’s use a DataTemplate. I also needed to display the DateOfBirth values in PersianCalendar, so I’m going to create a converter for that. The final xaml looks like this:
Unfortunately, unlike full version of .NET Framework, in beta version of Silverlight 4, there’s no PersianCalendar class! I’m not sure about the justification SL team has made in order to exclude this class, but I hope they’ll include it in the final version. I’ve opened a thread here, requesting this calendar to be added to SL 4, you may want to follow it up.
For now, to do the date conversion “sans” PersianCalendar, I’m going to use my old FarsiLibrary project, now supporting with Silverlight. It is in fact the same projects that existed for WinForm and WPF, now built against Silverlight. You can get it here if you’re interested.
Back to work, the date converted looks like this:
public class PersianDateConverter : IValueConverter
…and here’s the output.
You can get the sources of this demo from here.