Rob Kraft's Software Development Blog

Software Development Insights

BooleanToVisibility Converter – Yes, you must write your own for your Phone

Posted by robkraft on May 6, 2011

You must code your own BooleanToVisibilityConverter in Windows Phone 7 development as of May 2011.  That may change when Silverlight 4 can be used for Phone 7 development.  Until then, I recommend you write your own, and start with a good example like the ones found here:

http://compiledexperience.com/blog/posts/useful-calue-converters

I spent more time than I expected on this project attempting to use a boolean value in my viewmodel to control the visibility of an error message on the form.  What I learned is that I DO need to write my own Converter class that implements Iconverter.  I had the impression from many blog posts that this was built it, but it is not – at least not in Windows Phone 7 with Visual Studio 2010 after the NoDo updates and all Phone SDK updates available through April of 2010.  I did not have to write my own converter though, I just copied this code I found elsewhere on the net.  Then I had to make 3 entries in my XAML:

  1. xmlns:loc=”clr-namespace:MyAppNamespace” where MyAppNamespace is the namespace over the class containing my converting.  This entry was already in my XAML for other purposes – and of course “loc” is just a variable name that you can replace with anything.
  2. Add this little bit of code in the Xaml after the first big block that imports the namespaces.  The name BooleanToVisibilityConverter needs to be the class name of the class you created for this.
    <phone:PhoneApplicationPage.Resources>

<loc:BooleanToVisibilityConverter x:Key=”BooleanToVisibility” />

</phone:PhoneApplicationPage.Resources>

  1. Then use it on one to many Visibility properties with code like the following.  In this case, I have a property named SearchFactsFound on my ViewModel.

Visibility=”{Binding SearchFactsFound, Converter={StaticResource BooleanToVisibility}}”

Advertisements

One Response to “BooleanToVisibility Converter – Yes, you must write your own for your Phone”

  1. John said

    Thank you, your post filled in an important missing link for me. I’m trying to keep an open mind, but WPF is to date the worst language experience I can remember. I’m all for innovation but I’m apparently missing the point of why this approach is desirable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: