How to format TimeSpan in xaml

Posted by biju on Stack Overflow See other posts from Stack Overflow or by biju
Published on 2010-12-30T13:47:51Z Indexed on 2010/12/30 13:54 UTC
Read the original article Hit count: 241

I am trying to format a textblock which is bound to a TimeSpan property.It works if the property is of type DateTime but it fails if it is a TimeSpan .I can get it done using a converter.But i am trying to find out if there is any alternatives.

Sample Code

public TimeSpan MyTime { get; set; }

    public Window2()
    {
        InitializeComponent();
        MyTime = DateTime.Now.TimeOfDay;
        DataContext = this;
    }

Xaml

  <TextBlock Text="{Binding MyTime,StringFormat=HH:mm}"/>

I am expecting the textblock to show only hours and mintes.But it is showing like

19:10:46.8048860

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml