Mar 05 2010

Binding a Converter Parameter

There are a lot of situations when you need to bind ConverterParameter value.

Imagine that you have Receipt class with two fields: amount and currency type. And you need to format amount string to something like $1,000.00 or ¥1,000.00 depending on currency type. So the good idea is to use converter to do formatting.

The good way is to have something like AmountFormatter which takes amount and currency type and does the formatting.

Current version of the Silverlight disallowing us to bind Converter Parameter value.  But we could pass whole Reciept object to the formatter and take amount and currency type from it directly. Such formatter could look like this:

public class AmountConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var receipt = value as Receipt;
        if (receipt != null)
        {
            return String.Format("{1}{0:0,0.0}", receipt.Amount, receipt.CurrencyChar);
        }
		return value.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        ...
    }
}

This method is not good cause we tide converter and particular class (in our case it is Receipt). But what could we do?

Ok, we want to have a reusable converter. But we need to pass several values to it at the same time. Then lets simply define an interface which converter is expecting to get:

 

public class AmountConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var data = value as IConverterData
        if (data != null)
        {
            return String.Format("{1}{0:0,0.0}", data.Value, data.CurrencySign);
        }
		return value.ToString();
    }

	... 

	public interface IConverterData
    {
        string Value { get; set; }
        string CurrencySign { get; set; }
    }
}

Now to prepare Receipt class to be used in conjunction with AmountConverter we just need to implement AmountConverter.IConverterData interface.

And the usage will look like this:

<TextBlock Text="{Binding Converter={StaticResource AmountConverter}}"/>

Please not what we binds to the whole object here.

Feb 26 2010

Enable Silverlight 4 Tools in Visual Studio 2010 Release Candidate

Are you still waiting for Microsoft to release Silverlight 4 for Visual Studio 2010 RC?

I'm not anymore :)

You could completely use Silverlight 4 bits for VS 2010 Beta 2 with VS 2010 RC. Alex Sorokoletov has found how to install Silverlight 4 on VS 2010 RC.

There is no magic and steps are simple as One, Two, Tree:

  1. Download Silverlight 4 Tools For VS 2010 Beta 2
  2. Run installation. The dialog window saying what Visual Studio 2010 Beta 2 should be installed will appear. Don’t hit the cancel button!
  3. Locate folder named like ‘bfb0032a835647b79718f26ba81d3392’ on root of some of your hard drives. And copy it’s content to some other location.
  4. Open ParameterInfo.xml and comment out <BlockIf DisplayText="Visual Studio 2010 Beta 2 ..."> section (it is placed on lines 13-41)
  5. Now you are ready to SPInstaller.exe to install Silverlight 4 Tools
  6. One additional step is required to force Visual Studio to use Silverlight 4 instead of Silverlight 3 for visual designers. To do it open regedit and search for DesignerPlatforms. Under this key go to Silverlight and change SilverlightHost value to ‘4.0’.

You should be happy now :)

If you need RIA services then load patched Microsoft.RiaServices.Tools.dll assembly and copy it to ‘c:\Program Files\Microsoft SDKs\RIA Services\v1.0\Libraries\Server’ folder.

And then open GAC management console by [Win+R] -> assembly ->[Enter]. Remove old one assembly and add new one.

Restart Visual Studio and you are done.

Thanks for Alex!

Oct 20 2009

Resharper 5.0 First Public Build is now available

Uhh, it's a hot day: first the visual studio 2010 beta 2 has came out and now JetBrains released first public available build of the ReSharper 5.0 :)

Here is download page: http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+5.0+Nightly+Builds

What's New in ReSharper 5.0

  • External Sources
  • Structured Patterns
  • Project Refactorings
  • Call and Value Tracking
  • Internationalization
  • Visual Studio 2010 support (coming soon)
  • ASP.NET markup support
  • ASP.NET MVC support
  • Intellisense improvements
  • Bookmarks
  • Inspect project/folder
  • Upgrade-to-LINQ analysis
  • Native NUnit support
  • Xml Formatting

Here you could find more info about R# 5.0 futures: ReSharper 5.0 Overview

Oct 20 2009

Visual Studio 2010 beta 2 - Web Installer

Hi, now it is just 20th of October... but VS 2010 beta 2 will be available only at 21th :(

Can't wait? Ok, I have time machine for you:

Microsoft Visual Studio 2010 Ultimate Beta 2 — ENU (Web Installer)
http://go.microsoft.com/fwlink/?LinkId=151233

You are welcome! :)

Important: Remove ASP.NET MVC 1.1 beforeinstallation. If it does not want to uninstall saying "You first need to uninstall Microsoft Visual Studio 2010 Tools for MVC 1.1, then uninstall ASP.NET MVC 1.1 (this is the runtime component)" then delete next registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET\MVCTools 1.1 (if you run 64bit OS then that key will be under HKLM\SOFTWARE\Wow6432Node node)

P.S.

If you have problems with TFSObjectModel during uninstalling VS 2010 beta 1 then just uninstall it from Programs and Futures manually ;)

 

It's a great day! :)