views:

55

answers:

2

Well, I needed to bind DateTime.Now to a TextBlock, I used that:

 Text="{Binding Source={x:Static System:DateTime.Now},StringFormat='HH:mm:ss tt'}"

Now, how to force it to update? It get's the time when control is loaded and wouldn't update it...

+2  A: 

Edited (I didn't account for him wanting to auto-update):

Here's a link of a 'Ticker' class that uses INotifyPropertyChanged so it'll auto-update. Here's the code from the site:

namespace TheJoyOfCode.WpfExample
{
    public class Ticker : INotifyPropertyChanged
    {
        public Ticker()
        {
            Timer timer = new Timer();
            timer.Interval = 1000; // 1 second updates
            timer.Elapsed += timer_Elapsed;
            timer.Start();
        }

        public DateTime Now
        {
            get { return DateTime.Now; }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Now"));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}


<Page.Resources>
   <src:Ticker x:Key="ticker" />
</Page.Resources>

<TextBox Text="{Binding Source={StaticResource ticker}, Path=Now}"/>

Declare:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Now this will work:

<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}}" />
myermian
Wrong. This will not help. (This is exactly what he wrote)
SLaks
And why won't that work?
myermian
Because it still won't update. Read the question again.
SLaks
Ah, yea... I see that now.
myermian
Hey... thanks...
Ike
A: 

You need to make a timer that updates the textbox every second.

SLaks