Auto-Updating TextBlock for Silverlight

This textblock usercontrol updates its contents every second. Just set a binding to the text property and voila!

Note that the part inside the Dispatcher BeginInvoke method that gets the binding expression and sets the binding to the parent binding of the binding expression is equivalent to the missing .UpdateTarget() found in WPF.


<UserControl x:Class="CLARIA.Infrastructure.SL.RichClient.Controls.UpdatingTextBlock"
		d:DesignHeight="28" d:DesignWidth="240">
	<TextBlock Name="text" />


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Data;
using System.ComponentModel;

namespace CLARIA.Infrastructure.SL.RichClient.Controls
public partial class UpdatingTextBlock : UserControl, IDisposable
public UpdatingTextBlock()

// Set timer
if (!DesignerProperties.GetIsInDesignMode(this))
t = new Timer(new TimerCallback(TimerCallbackMethod), null, 0, 1000);

Timer t;
BindingExpression exp;

public string Text
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }

// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc…
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(“Text”, typeof(string), typeof(UpdatingTextBlock), new PropertyMetadata(“”));

void TimerCallbackMethod(object target)
Dispatcher.BeginInvoke(() =>
if (exp == null)
exp = GetBindingExpression(UpdatingTextBlock.TextProperty);
if (exp != null)
SetBinding(UpdatingTextBlock.TextProperty, exp.ParentBinding);
text.Text = this.Text;


#region IDisposable Members

bool _disposed = false;

public void Dispose()
if (!_disposed)
if (t != null)
_disposed = true;



3 thoughts on “Auto-Updating TextBlock for Silverlight

    1. The previous post had an extension method to convert a datetime/timespan into a facebook-like time, so I made a valueconverter and have this to show times for notes placed on an object and how long ago the note was created.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s