Friday, February 6, 2009

WPF control that shows today's date

All I wanted was to show today's date on a screen. It took a few minutes of Googling to put the pieces together.

Here's the short and skinny:
<TextBlock Text="{Binding Source={x:Static System:DateTime.Now}, StringFormat=\{0:MM/dd/yyyy\}}"></TextBlock>

Just to be sure, here's how it would fit in if you were to start up a brand new WPF project and paste the TextBlock into a Window:
<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<TextBlock Text="{Binding Source={x:Static System:DateTime.Now}, StringFormat=\{0:MM/dd/yyyy\}}"></TextBlock>
</Window>

Here are a few places from which I have pieced together this minor epiphany: