Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Even if you could declare DateTime.Now in Silverlight's XAML (since you can in WPF - <a href="http://soumya.wordpress.com/2010/02/12/wpf-simplified-part-11-xaml-tricks/" rel="nofollow noreferrer">http://soumya.wordpress.com/2010/02/12/wpf-simplified-part-11-xaml-tricks/</a>), you have the issue that your time won't update. If you use a local timer that updates on the second you can ensure that your time will update as well.</p> <pre><code>public class LocalTimer : INotifyPropertyChanged { private DispatcherTimer timer; public LocalTimer() { timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(1.0); timer.Tick += new EventHandler(TimerCallback); this.TimeFormat = "hh:mm:ss"; this.DateFormat = "dddd, MMMM dd"; } private void TimerCallback(object sender, EventArgs e) { PropertyChanged(this, new PropertyChangedEventArgs("FormattedDate")); PropertyChanged(this, new PropertyChangedEventArgs("FormattedTime")); } public bool Enabled { get { return this.timer.IsEnabled; } set { if (value) this.timer.Start(); else this.timer.Stop(); } } public string FormattedDate { get { return DateTime.Now.ToString(this.DateFormat); } set {} } public string FormattedTime { get { return DateTime.Now.ToString(this.TimeFormat); } set{} } public string TimeFormat { get; set; } public string DateFormat { get; set; } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion } </code></pre> <p>Declare an instance of this in xaml ala:</p> <pre><code>&lt;local:LocalTimer x:Key="theTime" Enabled="True" /&gt; </code></pre> <p>and use the binding to ensure that your time is always reflected.</p> <pre><code>&lt;TextBlock Text="{Binding Source={StaticResource theTime}, Path=FormattedDate, Mode=OneWay}" x:Name="TodaysDate" /&gt; &lt;TextBlock Text="{Binding Source={StaticResource theTime}, Path=FormattedTime, Mode=OneWay}" x:Name="CurrentTime" /&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload