shanmugharaj
shanmugharaj

Reputation: 3924

How to format converter parameter in WPF

My XAML code is below:

<TextBox
    Text="{Binding Path=TimeDuration, ConverterParameter='hhmm',
                   Converter={StaticResource HoursMinutesTimeSpanConverter}}"/>

Code for converter:

[ValueConversion(typeof(TimeSpan), typeof(String))]
public class HoursMinutesTimeSpanConverter : IValueConverter
{
    /// <summary>Converts the value into a time only formatted string.</summary>
    /// <param name="value"> The value. </param>
    /// <param name="targetType"> The target type. </param>
    /// <param name="parameter"> The parameter. </param>
    /// <param name="culture"> The culture. </param>
    /// <returns>Time string value. </returns>
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        return ((TimeSpan)value).ToString(parameter.ToString());
    }

    /// <summary>Converts the hours minutes string back to
    /// the time span value.</summary>
    /// <param name="value"> The value. </param>
    /// <param name="targetType"> The target type. </param>
    /// <param name="parameter"> The parameter. </param>
    /// <param name="culture"> The culture. </param>
    /// <returns>Time string value. </returns>
    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        return TimeSpan.ParseExact((string)value, parameter.ToString(),
            CultureInfo.CurrentCulture);
    }
}

It works fine. Actually I need to give this hh\:mm as parameter for proper formatting in the converter.

I tried this:

>  ConverterParameter='hh{escape character code here}mm' giving codes for the characters

Note : I typed that way since that's a code in html its not reflecting in my posts so why.

By referring this: link.

Its not working in my case. How to solve this.

Thanks in advance.

Upvotes: 1

Views: 1373

Answers (4)

Nikita Shrivastava
Nikita Shrivastava

Reputation: 3018

You can get that output by using this StringFormat:

<TextBox Text="{Binding TimeDuration,
                StringFormat={}{0:hh}:{0:mm}, FallbackValue=00:00}" />

Update :

TimeSpan represents a time interval not a time of day,so it is always 24 hrs format & does not have a format for HH.

Whereas, with DateTime , hh will display hours in a 12 hour format and you should use HH if you want 24 hour format.

Also, the TimeDuration i have considered is of type TimeSpan.

Upvotes: 4

Amit
Amit

Reputation: 46351

From the docs:

The custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals. For example, "dd.hh:mm" defines a period (.) as the separator between days and hours, and a colon (:) as the separator between hours and minutes.

Use like this:

Text="{Binding Path=TimeDuration,
                            ConverterParameter='hh\\:mm',
                            Converter={StaticResource HoursMinutesTimeSpanConverter}}

This way you'll be able to use whatever separator you like.

Note: It is required to escape '\' with a double-slash ('\\') since the ConverterParameter value is in "code space" and follows normal C# string literal rules, and not in "markup-space" as in the link in the question.

Upvotes: 1

shanmugharaj
shanmugharaj

Reputation: 3924

I figured it.

It should be this way

ConverterParameter='hh\\:mm'

Two back slashes in the string. And also as Nikita said no need of converters. That approach is fine.

Upvotes: 0

Chui Tey
Chui Tey

Reputation: 5574

Colons need to be escaped with a backslash i.e.

TimeSpan.FromHours(5.25).ToString(@"hh\:mm")

Try this:

/// <summary>Converts the value into a time only formatted string.</summary>
/// <param name="value"> The value. </param>
/// <param name="targetType"> The target type. </param>
/// <param name="parameter"> The parameter. </param>
/// <param name="culture"> The culture. </param>
/// <returns>Time string value. </returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var escapedFormatString = parameter.replace(@":", @"\:");
    return ((TimeSpan)value).ToString(escapedFormatString);
}

and this should work:

<TextBox Text="{Binding Path=TimeDuration,
                        ConverterParameter='hh:mm',
                        Converter={StaticResource HoursMinutesTimeSpanConverter}}"/>

Upvotes: 0

Related Questions