WPF Binding FallbackValue set to Binding

前端 未结 3 1101
谎友^
谎友^ 2021-01-31 15:43

Is there a way to have another binding as a fallback value?

I\'m trying to do something like this:

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 16:19

    If you run into problems with binding to null values and PriorityBinding (as Shimmy pointed out) you could go with MultiBinding and a MultiValueConverter like that:

    public class PriorityMultiValueConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            return values.FirstOrDefault(o => o != null);
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Usage:

    
        
            
                
                
            
        
    
    

提交回复
热议问题