开发者

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

开发者 https://www.devze.com 2023-01-07 08:51 出处:网络
I have a PriorityBinding <PriorityBinding FallbackValue=\"Bindings were null\"> <Binding Path=\"Foo\" />

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 ?? Depende开发者_StackOverflow中文版ncyProperty.UnsetValue;
    }

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号