0

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?

4 Answers 4

3

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}...
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

if you're doing MVVM, make sure in your setter for Property you raise the property notification for both Property and PropertyDescriptor
+1 Prefer this approach over using ValueConverter or Trigger, since you can easily write a test to verify that the Property is being displayed correctly.
1

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.

2 Comments

good call. also useful is .FallbackValue for when the binding is unable to return a value
-1 Specifically to the question asked, this won't work. He said it was an empty string, not a null value.
0
    public string MyProperty 
    { 
        get
        {
            if (String.IsNullOrEmpty(_myProperty))
                return "n/a";
            else
                return _myProperty;
        }
        set 
        {
            if (_myProperty != value)
            { 
                _myProperty = value;
                RaisePropertyChanged("MyProperty")
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.