DataTrigger where value is NOT null?

后端 未结 12 631
野趣味
野趣味 2020-11-27 10:56

I know that I can make a setter that checks to see if a value is NULL and do something. Example:


  
    

        
12条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 11:00

    You can use an IValueConverter for this:

    
        
            
        
        
            
        
    
    

    Where IsNullConverter is defined elsewhere (and conv is set to reference its namespace):

    public class IsNullConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value == null);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new InvalidOperationException("IsNullConverter can only be used OneWay.");
        }
    }
    

    A more general solution would be to implement an IValueConverter that checks for equality with the ConverterParameter, so you can check against anything, and not just null.

提交回复
热议问题