Binding timespan in wpf mvvm, and show only minutes:seconds?
Asked Answered
A

2

7

I want to be able to show minutes and seconds in a textbox. The TextBox is bound towards a property, which is a TimeSpan. In the textbox, the default is : "00:00:00".

This works fine, but how to show only 00:00 where the hours portion is removed.

How do I do this?

This is my binding:

public TimeSpan MaxTime
{
    get
    {
        if (this.Examination.MaxTime == 0)
            return TimeSpan.Zero;
        else
            return maxTime;
    }
    set
    {
        maxTime = value;
        OnPropertyChanged("MaxTime");
        TimeSpan x;
        this.Examination.MaxTime = int.Parse(maxTime.TotalSeconds.ToString());                              
    }
} 
<TextBox Text="{Binding Path=MaxTime,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
Antislavery answered 12/5, 2011 at 7:33 Comment(0)
M
17

If you just wanted one way binding, then you could use StringFormat:

 <TextBlock Text="{Binding MaxTime, StringFormat={}{0:hh':'mm':'ss}}" />

If you want bidirectional binding, then I would go for a custom value converter.

[ValueConversion(typeof(TimeSpan), typeof(String))]
public class HoursMinutesTimeSpanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
                          Globalization.CultureInfo culture)
    {
        // TODO something like:
        return ((TimeSpan)value).ToString("hh':'mm':'ss");
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                              Globalization.CultureInfo culture)
    {
        // TODO something like:
        return TimeSpan.ParseExact(value, "hh':'mm':'ss", CultureInfo.CurrentCulture);
    }
}
Motionless answered 12/5, 2011 at 7:40 Comment(5)
Escaping the colon in the format string didn't work for me. I had to change it to {0:hh':'mm}, that is: <TextBlock Text="{Binding MaxTime, StringFormat={}{0:hh':'mm}}" />Usurpation
or StringFormat={}{0:hh\:mm}Seventy
Just how does this only show minutes and seconds? I understand hh to be hour, and mm to be minute.Nevins
@Nevins then use "mm:ss" or whatever format you need.Motionless
I had the same problem as @sthlm58. Is it worth adding that into this answer?Rockling
A
0

I took a different tact and created a string called TimeElapsed and during a timer tick, I would look at another property which was a StopWatch (set during the timer) and then if changed set the string time for showing on the screen.

private Stopwatch StopWatch { get; set; }
private double TimeElapsedDouble { get; set; }

private string _TimeElapsed;

public string TimeElapsed
{
    get { return _TimeElapsed; }
    set { _TimeElapsed = value; OnPropertyChanged(nameof(TimeElapsed)); }
}
private void timerTick(object sender, EventArgs e)
{
    var timeElapsedDuration = StopWatch.Elapsed.Duration(); // Get the timespan.

    if (TimeElapsedDouble != timeElapsedDuration.TotalSeconds) // A change happened?
    {
        TimeElapsedDouble = timeElapsedDuration.TotalSeconds;
        TimeElapsed = timeElapsedDuration.ToString(@"mm\:ss"); // Answer is here
    }

}

Then just bind it to the control as needed

<Label Content="{Binding TimeElapsed}" />
Aiello answered 4/3, 2021 at 3:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.