StringFormat on Binding

后端 未结 9 636
野的像风
野的像风 2021-01-01 13:17

View:


I want to format the Date to \"dd/MM/yyyy\", in other words, without the time.

I

9条回答
  •  星月不相逢
    2021-01-01 13:41

    The best and the easiest way would be to use a converter to which you pass the Date and get the formatted string back. In e.g. MyNamespace.Converters namespace:

    public class DateFormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
                return null;
    
            DateTime dt = DateTime.Parse(value.ToString());
            return dt.ToString("dd/MM/yyyy");
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    And in your xaml just reference the converter and add the following converter:

    xmlns:conv="using:MyNamespace.Converters" 
    

    in your xaml page and in page.resources add this

    
    
    
    

提交回复
热议问题