How to boolean && two visibility converters

前端 未结 4 1282
太阳男子
太阳男子 2020-12-10 00:41

I have two separate converters for visibility, one based on whether a field has been updated and one based on whether a field is allowed to be seen. I use the updatedField

4条回答
  •  天命终不由人
    2020-12-10 01:08

    You could use a MultiBinding together with a short, hand made IMultiValueConverter.

    Example:

    
        
            
        
        
        
        
            
                
                    
                    
                
            
                           
    
    

    ... and the converter ...

    class MultiBooleanToVisibilityConverter : IMultiValueConverter
    {
        public object Convert(object[] values,
                                Type targetType,
                                object parameter,
                                System.Globalization.CultureInfo culture)
        {
            bool visible = true;
            foreach (object value in values)
                if (value is bool)
                    visible = visible && (bool)value;
    
            if (visible)
                return System.Windows.Visibility.Visible;
            else
                return System.Windows.Visibility.Hidden;
        }
    
        public object[] ConvertBack(object value,
                                    Type[] targetTypes,
                                    object parameter,
                                    System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

提交回复
热议问题