I have a class in which the name of the object can be null.
public class Thing
{
/// <summary>
/// The identifier of the thing
/// </summary>
/// <remarks>
/// This will never be null.
/// </remarks>
public string Identifier { get; set; }
/// <summary>
/// The name of the thing
/// </summary>
/// <remarks>
/// This MAY be null. When it isn't, it is more descriptive than Identifier.
/// </remarks>
public string Name { get; set; }
}
In a Silverlight ListBox, I use a DataTemplate where I have the name bound to a TextBlock:
<DataTemplate x:Key="ThingTemplate">
<Grid>
<TextBlock TextWrapping="Wrap" Text="{Binding Name}" />
</Grid>
</DataTemplate>
However, this obviously doesn’t look very good if the Name is null. Ideally, I would want to use something equivalent to
string textBlockContent = thing.Name != null ? thing.Name : thing.Identifier;
but I can’t change my model object. Is there any good way to do this?
I thought about using a Converter, but it seems to me I’d have to bind the converter to the object itself, and not the Name property. This would be fine, but how would I then rebind when either Name or Identifier changes? IValueConverter doesn’t appear to have any way to force a reconvert if I would manually listen on my object’s INotifyPropertyChanged event.
Any ideas on the best way to do this?
In WPF, you can do this easily by implementing an IMultiValueConverter. Unfortunately, Silverlight doesn’t support this directly, though there are workarounds written for Silverlight.