How to bind a Localized string in the TargetNullValue attribute?

前端 未结 2 926
耶瑟儿~
耶瑟儿~ 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:06

    I don't see any way to do that with TargetNullValue. As a workaround, you can try using a converter:

    public class NullValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                return value;
            }
    
            var resourceName = (string)parameter;
    
            return AppResources.ResourceManager.GetString(resourceName);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Then add it to the resources of your page:

    
        
    
    

    Finally, use it instead of TargetNullValue:

    
    

提交回复
热议问题