如果返回的值为Null,如何使PriorityBinding失败?

时间:2022-12-08 20:46:00

I have a PriorityBinding

我有一个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.

我想这样做,如果Foo为null,它将使用Bar,如果两者都为null,它将使用FallbackValue。但是null是此属性的有效值,因为它只需要一个对象。

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.

当值为null时,有没有办法使PriorityBinding前进到下一个绑定?我宁愿在XAML中这样做,但如果我不能,我只会为它做一个转换器。

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();
    }
}

1 个解决方案

#1


5  

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

如果绑定值为null,我将使用valueconverter返回UnsetValue。

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

如果要在不同类型的对象之间共享数据模板,PriorityBindings更有用。

#1


5  

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

如果绑定值为null,我将使用valueconverter返回UnsetValue。

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

如果要在不同类型的对象之间共享数据模板,PriorityBindings更有用。