views:

327

answers:

4

I have a collection of objects that I wish to bind to a ListView control. In some of the objects, the value of a property that will be displayed in a column in the ListView is an empty string (""). I want to replace the empty string ("") with "n/a" automatically using binding.

How can I accomplish this?

A: 

You could always add a read only property to your bound object that formatted what you wanted to display.

public string Property
{
  get;
  set;
}
public string PropertyDescriptor
{
  get
  {
    if (string.IsNullOrEmpty(this.Property))
      return "n/a";
    else
      return this.Property;
  }
}

This works quite well if you're using MVVM.

opedog
if you're doing MVVM, make sure in your setter for Property you raise the property notification for both Property and PropertyDescriptor
qntmfred
Of course, I was just trying to be brief.
opedog
A: 
    public string MyProperty 
    { 
        get
        {
            if (String.IsNullOrEmpty(_myProperty))
                return "n/a";
            else
                return _myProperty;
        }
        set 
        {
            if (_myProperty != value)
            { 
                _myProperty = value;
                RaisePropertyChanged("MyProperty")
            }
        }
    }
qntmfred
+3  A: 

Define a value converter:

class EmptyToN_AConverter : IValueConverter
{
    public object Convert(
        object value, 
        Type targetType, 
        object parameter, 
        System.Globalization.CultureInfo culture)
    {
        string s = value.ToString();
        if (string.IsNullOrEmpty(s)) return "N/A";
        return s;
    }

    public object ConvertBack(
        object value, 
        Type targetType, 
        object parameter, 
        System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Example XAML:

<Window.Resources>
  ...
    <local:EmptyToN_AConverter x:Key="NAConverter"/>
</Window.Resources>

...{Binding Path=TheProperty, Converter={StaticResource NAConverter}}...

You may even parameterize the converter and expose the "N/A" in XAML:

    public object Convert(
        object value, 
        Type targetType, 
        object parameter, 
        System.Globalization.CultureInfo culture)
    {
        string s = value.ToString();
        if (string.IsNullOrEmpty(s)) return parameter.ToString();
        return s;
    }

...{Binding Path=TheProperty, 
            Converter={StaticResource NAConverter}, 
            ConverterParameter=N/A}...
Aviad P.
Thanks! This worked perfectly for me.
joek1975
+1  A: 

Use the BindingBase.TargetNullValue property :

<GridViewColumn DisplayMemberBinding="{Binding MyProperty, TargetNullValue=N/A}"/>

EDIT: as pointed out by Aviad, this will only work for a null value, not an empty string. I don't delete this answer because it can still be useful to others.

Thomas Levesque
good call. also useful is .FallbackValue for when the binding is unable to return a value
qntmfred
-1 Specifically to the question asked, this won't work. He said it was an empty string, not a null value.
Aviad P.
@Aviad : good point, I misread the question...
Thomas Levesque