7

I have a PriorityBinding

<PriorityBinding FallbackValue="Bindings were null">
    <Binding Path="Foo" />
    <Binding Path="Bar" />
</PriorityBinding>

I'd like to make it so if Foo is null it will use Bar and if both are null it will use the FallbackValue. However null is a valid value for this property because it only expects an object.

Is there any way to make the PriorityBinding advance to the next binding when the value is null? I'd prefer to do it in XAML, but if I can't I'll just make a converter for it.

Edit

I ended up just writing a converter for it

public class NullToDependencyPropertyUnsetConverter 
    : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value ?? DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Mike Fuchs
  • 12,081
  • 6
  • 58
  • 71
Bryan Anderson
  • 15,969
  • 8
  • 68
  • 83
  • Look at this: http://stackoverflow.com/questions/6947728/equiv-to-coalesce-in-xaml-binding – Jerry Nixon Aug 25 '11 at 00:19
  • I was wondering about this sort of thing. My objects have a lot of non-null properties, so when I try to use PriorityBinding, I end up getting blank values instead of the next one down the list. – Patrick Feb 06 '13 at 14:33

1 Answers1

5

I'd go with the valueconverter returning UnsetValue if the bound value is null.

PriorityBindings are more useful if you want to share a datatemplate between different types of objects.

Goblin
  • 7,970
  • 3
  • 36
  • 40