How to bind a Localized string in the TargetNullValue attribute?

前端 未结 2 945
耶瑟儿~
耶瑟儿~ 2021-01-12 20:34

I have a Textblock which Text attribute is binding to a DateTime? type data, and I want to show something when the DateTime? data is null.
The code below works great.<

2条回答
  •  耶瑟儿~
    2021-01-12 21:28

    Since you can't have a binding inside another binding you will need to use a multi-binding.

    Something like:

    
        
    
    
    
        
            
                
                
                
            
        
    
    
    public class NullConverter : IMultiValueConverter
    {
        #region Implementation of IMultiValueConverter
    
        public object Convert(object[] values, Type targetType, 
                              object parameter, CultureInfo culture)
        {
            if (values == null || values.Length != 2)
            {
                return string.Empty;
            }
    
            return (values[0] ?? values[1]).ToString();
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, 
                                    object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    
        #endregion
    }
    

提交回复
热议问题