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

时间:2010-07-14 17:53:18

标签: wpf

我有PriorityBinding

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

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

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

修改

我最后只为它写了一个转换器

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 个答案:

答案 0 :(得分:5)

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

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