7/18/2008 7:14:59 PM
Yesterday I posed a question about conditional syntax and how one would accomplish this in xaml / silverlight pages & controls. There were no responses and I'm attributing that to the fact that it is a Friday. Anyway, after a day of much next-level blog-reading and trial-and-error I have come up with a solution that might not be the most elegant but it works.
I'm going to use a converter utility to provide this functionality. This will be a class that implements IValueConverter. This is is an interface that allows one to apply custom logic to a binding which is pretty much what I wanted to do in the first place.
If you are familiar with Scott Guthrie's 8-part series on Silverlight (which is fairly outdated btw with the new release of Beta 2) I will be building on the Digg example from that as that is the most pertinent "Hello World"'ey example I can think of.
So anyone familiar with Digg will know that there is a little box on the left of a story that displays how many diggs a story has.

That screenshot is from the example I built awhile ago following Scott's tutorial. See how even if a story has 1 or 2 the text below still says "diggs" ? With the example I will show you can customize that to where if a story has more than 1 digg it will read "diggs" or else it will simply read "digg". It sounds like a very trivial task (and it kind of is) but not anywhere near as trivial as with ASP.NET.
First add a class file to your SilverlightUI class library and name it something like NumberOfDiggsConverter.cs (what I did).
Here's my class. It's very important to implement the IValueConverter interface.
using
System;
using System.Windows.Data;
using System.Globalization;
namespace
SilverlightSandbox.Converters
{
public class NumberOfDiggsConverter : IValueConverter
{
public object Convert(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
return System.Convert.ToInt32(value) > 1 ? "diggs" : "digg";
}
public object ConvertBack(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Easy enough. Notice I didn't check the value parameter for null or anything fancy. Again, just a quick example and if you use something like this in production you might want to think about something a little better.
Now that we have our converter class we should register it and use it within our xaml user control.
Here is what my xaml looks like.
BEFORE REGISTERING CONVERTER
<
UserControl x:Class="SilverlightSandbox.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
AFTER REGISTERING CONVERTER
<
UserControl x:Class="SilverlightSandbox.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Converters="clr-namespace:SilverlightSandbox.Converters">
Now create a UserControl.Resources section. I added mine at the top of my page right before my main grid.
<UserControl.Resources>
<Converters:NumberOfDiggsConverter x:Key="DiggConverter" />
</UserControl.Resources>
The next, and final, step is to actually implement this. In our case we will set the text property of the digg block to the NumberOfDiggs property of my Digg object. I'll show the before and after syntax of the # of diggs text block.
BEFORE
<
TextBlock Text="diggs" />
AFTER
<
TextBlock Text="{Binding Diggs, Converter={StaticResource DiggConverter}}" />
Here's the final result:

Easy.
Silverlight
