0

So i have simple Label that i am update via Timer:

<Label Name="lblElapsedTime"
       Content="{Binding Converter={StaticResource ElapsedTimeConverter}}"/>

Timer tick:

StopWatch StopWatch = new StopWatch();
private void progressTimer_Tick(object sender, EventArgs e)
{ 
    lblElapsedTime.Content = string.Format("{0:00}:{1:00}:{2:00}:{3:00}:{4:00}",
    StopWatch.Elapsed.Days,
    StopWatch.Elapsed.Hours,
    StopWatch.Elapsed.Minutes,
    StopWatch.Elapsed.Seconds,
    StopWatch.Elapsed.Milliseconds / 10);
}

Now i want to replace this Timer and maybe write simple Converter to update my Label. Any ideas how to implement this ?

Currently i wrote this inside Window.Resources:

<Convertors:ElapsedTimeConverter x:Key="ElapsedTimeConverter"/>

And i have of course this Converter but is is it possible to update my StopWatch after restart it without Timer ?

This is my convert:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "";
        }

The problem is that although i binding this Convert into my Label this Convert not even executed.

user2908206
  • 265
  • 3
  • 16
  • can add your converter code? – pix Jan 08 '18 at 09:09
  • Currently its empty – user2908206 Jan 08 '18 at 09:11
  • Please see my Label code edit, i add my converter but this Convert function not even executed – user2908206 Jan 08 '18 at 09:13
  • Convert added into my question – user2908206 Jan 08 '18 at 09:15
  • 1
    `Content="{Binding Converter={StaticResource TimePropety, ElapsedTimeConverter}}"/>` then in your `progressTimer_Tick` method set the `TimeProperty` and send `NotificationPropertyChanged` event on the Setter. – pix Jan 08 '18 at 09:16
  • Is it possible to do that without timer ? – user2908206 Jan 08 '18 at 09:25
  • I do not now what you want to achieve. Why are you using a timer? That is the requirement behind the scene? – pix Jan 08 '18 at 09:26
  • No, i want to use only XAML if this is possible thats why i asked if i can achieve it without timer – user2908206 Jan 08 '18 at 09:28
  • have a look here. XAML Is only presentation stuff, you will have to use some C# code. https://stackoverflow.com/questions/16921711/displaying-the-time-in-the-local-time-zone-in-wpf-xaml – pix Jan 08 '18 at 09:33
  • OK but i am not understand how to achieve that, why if i add my converter into my label my convert function not executed ? because if it does i can return whatever i want to from my class property which i have acceess from this convert function – user2908206 Jan 08 '18 at 09:39
  • Close your VS for now, and read more documentation about MVVM pattern, XAML biding, and WPF in general. This will be a good start. A converter is call only when you property is updated and/or binded. same for the convertback. – pix Jan 08 '18 at 09:45
  • For a cyclically repeated action, you'll inevitably need some kind of timer. It doesn't matter if the timer tick handler directly updates a UI element, or if it updates a view model property that is the source of a Binding. – Clemens Jan 08 '18 at 09:55
  • OK thanks a lot for your help! – user2908206 Jan 08 '18 at 10:12

0 Answers0