I ended up using the following:
public static class WindowTitleBehavior
{
public static readonly DependencyProperty WindowTitleProperty = DependencyProperty.RegisterAttached(
"WindowTitleProperty", typeof(string), typeof(UserControl),
new FrameworkPropertyMetadata(null, WindowTitlePropertyChanged));
public static string GetWindowTitle(DependencyObject element)
{
return (string)element.GetValue(WindowTitleProperty);
}
public static void SetWindowTitle(DependencyObject element, string value)
{
element.SetValue(WindowTitleProperty, value);
}
private static void WindowTitlePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UserControl control = d as UserControl;
if (!control.IsLoaded)
{
control.Loaded += new RoutedEventHandler(setTitle);
}
setTitle(control);
}
private static void setTitle(object sender, RoutedEventArgs e)
{
UserControl control = sender as UserControl;
setTitle(control);
control.Loaded -= new RoutedEventHandler(setTitle);
}
private static void setTitle(UserControl c)
{
Window parent = UIHelper.FindAncestor<Window>(c);
if (parent != null)
{
parent.Title = (string)WindowTitleBehavior.GetWindowTitle(c);
}
}
}
Which makes use of Philipp Sumi's code snippet to find the first ancestor Window: http://www.hardcodet.net/2008/02/find-wpf-parent
In my views I can now do:
<UserControl Behaviors:WindowTitleBehavior.WindowTitle="My Window Title">
And it sets the title of the containing Window.