Binding your MVVM model to an AttachedProperty



Given the following section with special attached Property Title, you would think this would just work.  Well it doesn't, you need to handle the attached property manually.

<
local:MySection x:Name="TitleSection" Title="{Binding Name}">

Here is the Property Name in the DataContext  in the ViewModel

private string _name;

public string Name
{
 
 get {return _name;}
}


Below is the attached property code for Title:


public String Title
{
get { return (String)GetValue(TitleProperty); }
set
{
SetValue(TitleProperty,
value);
Title_TextBlock.Text = (
value as String).ToUpper();
}
}

Below is the Attached Property of Title defined:
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(String), typeof(DossierSection), new PropertyMetadata(new PropertyChangedCallback(OnTitlePropertyChanged)));


If I handle the PropertyChanged event through the callback and assign the title directly, it forces the update in the XAML:

private
static void OnTitlePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((d is MySection) == false) return;
(d
as MySection).Title = e.NewValue as string;
}

Strangely, even though (d as MySection).Title already seems to have the right value, the view never hears about it, unless you reassign it (even though it is part of the view!)