Equiv. to Coalesce() in XAML Binding?

前端 未结 3 1297
深忆病人
深忆病人 2021-01-03 01:24

In SQL I can do this:

Select Coalesce(Property1, Property2, Property3, \'All Null\') as Value
From MyTable 

If Property1, 2 and 3 are all n

3条回答
  •  太阳男子
    2021-01-03 01:59

    You'd have to build a custom IMultiValueConverter to do that and use a MultiBinding. PriorityBinding uses the first binding in the collection that produces a value successfully. In your case, the Property1 binding resolves immediately, so it's used. Since Property1 is null, the TargetNullValue is used.

    A converter like this:

    public class CoalesceConverter : System.Windows.Data.IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, 
                object parameter, System.Globalization.CultureInfo culture)
        {
            if (values == null)
                return null;
            foreach (var item in values)
                if (item != null)
                    return item;
            return null;
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, 
                object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    And MultiBinding like this:

    
        
        
    
    
    
        
            
                
                
                
            
        
    
    

提交回复
热议问题