Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a PriorityBinding fail if the value returned is Null?

Tags:

wpf

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();
    }
}
like image 700
Bryan Anderson Avatar asked Jul 14 '10 17:07

Bryan Anderson


1 Answers

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.

like image 155
Goblin Avatar answered Nov 12 '22 13:11

Goblin