How to use StringFormat in XAML elements?

前端 未结 4 2023
难免孤独
难免孤独 2021-01-31 07:20

I\'m deep in a XAML stack of elements binding to orders.

The order date displays as e.g. \"12/31/2008 12:00:00 AM\".

I want it to display as e.g. \"31.12

4条回答
  •  粉色の甜心
    2021-01-31 08:05

    XAML

    
        
    
    
    
    

    C#

    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value,
                           Type targetType,
                           object parameter,
                           CultureInfo culture)
        {
            if (value != null)
            {
                return ((DateTime)value).ToString("dd.MM.yyyy");
            }
            else
            {
                return String.Empty;
            }
        }
    
        public object ConvertBack(object value,
                                  Type targetType,
                                  object parameter,
                                  CultureInfo culture)
        {
            return DateTime.Parse(value.ToString());
        }
    }
    

提交回复
热议问题