Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Binding in Silverlight requires a Source object or a Dependency object. From that source object you can bind to Properties (hence by definition you are binding to instance members) or Dependency Properties.</p> <p>Since <code>DateTime.Now</code> is a static property you cannot bind to it in Silverlight directly, hence some code is needed. The next best thing is to use code to:-</p> <ul> <li>ensure as much of what you need can be expressed in XAML</li> <li>to do so in an as de-coupled manner as possible.</li> </ul> <p>Hence we can analyse that we need two things.</p> <ol> <li>Expose the static members of DateTime as instance properties of some object</li> <li>Have some way to format the DateTime to a desirable output.</li> </ol> <p>To handle the first item I would create a <code>StaticSurrogate</code> class, where I would create instance properties for the static properties that we need access to:-</p> <pre><code>public class StaticSurrogate { public DateTime Today { get { return DateTime.Today; } } public DateTime Now { get { return DateTime.Now; } } } </code></pre> <p>Now we need a way to format a Date time. A value converter is the right tool for this job, borrowing heavily from this <a href="http://timheuer.com/blog/archive/2008/07/30/format-data-in-silverlight-databinding-valueconverter.aspx" rel="noreferrer">Tim Heuer Blog</a> :-</p> <pre><code>public class FormatConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (parameter != null) { string formatterString = parameter.ToString(); if (!String.IsNullOrEmpty(formatterString)) { return String.Format(culture, String.Format("{{0:{0}}}", formatterString), value); } } return (value ?? "").ToString(); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } </code></pre> <p>With these two classes in place we can now do the rest in Xaml, first we need instances of these classes in our resources:-</p> <pre><code>&lt;UserControl.Resources&gt; &lt;local:StaticSurrogate x:Key="Static" /&gt; &lt;local:FormatConverter x:Key="Formatter" /&gt; &lt;/UserControl.Resources&gt; </code></pre> <p>Now we can wire up the <code>TextBlock</code> :-</p> <pre><code>&lt;TextBlock Text="{Binding Today, Source={StaticResource Static}, Converter={StaticResource Formatter}, ConverterParameter='dd MMM yyy'}" /&gt; </code></pre> <p>Note that this approach has the following advantages:-</p> <ul> <li>we do not need to add code to the UserControl on which the TextBlock is placed, nor do we have to fiddle around with any data context.</li> <li>The Static resources could be placed in the App.Resources which would make the creation of the TextBlock entirely independent of having to add anything else to the UserControl.</li> <li>The formatting used to display the date can be independently modified.</li> <li>Access to additional static properties can easily be added to the <code>StaticSurrogate</code> class.</li> </ul>
 

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