StringFormat on Binding

后端 未结 9 635
野的像风
野的像风 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:33

    There is no property named StringFormat in Binding class. You can use Converter and ConverterParameter to do this. You can refer to Formatting or converting data values for display.

    For example here, I bind the date of a DatePicker to the text of a TextBlock.

    XAML:

    
        
            
        
        
        
    
    

    code behind, the DateFormatter class:

    public class DateFormatter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var a = language;
            // Retrieve the format string and use it to format the value.
            string formatString = parameter as string;
            if (!string.IsNullOrEmpty(formatString))
            {
                return string.Format(formatString, value);
            }
    
            return value.ToString();
        }
    
        // No need to implement converting back on a one-way binding
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return DependencyProperty.UnsetValue;
        }
    }
    

提交回复
热议问题