The Header class does not provide a DataContext property because it does not derive from FrameworkElement. The Header class is presented in diagrams by instances of the LaneGridHeaderPresenter class. The LaneGridHeaderPresenter is a FrameworkElement-derived class but its DataContext is already used internally by the Diagram control and assigning a custom DataContext will break the lane grid.
However, you can still associate your data with the presenter through an attached property and then bind to this attached property from the presenter's template. Here is how:
1. Assuming that you want to associate an object of type Person with the header, declare a new attached property of type Person in a convenient for you place. I am using a new class for this purpose, called PersonAttacher.
public class PersonAttacher : DependencyObject
{
public static Person GetPerson(DependencyObject obj)
{
return (Person)obj.GetValue(PersonProperty);
}
public static void SetPerson(DependencyObject obj, Person value)
{
obj.SetValue(PersonProperty, value);
}
public static readonly DependencyProperty PersonProperty =
DependencyProperty.RegisterAttached("Person", typeof(Person),
typeof(PersonAttacher), new PropertyMetadata(default(Person)));
}
2. Handle the Diagram.LaneGridHeaderCreated event. Attach an instance of the Person class to the header presenter:
void diagram_LaneGridHeaderCreated(object sender, GridHeaderEventArgs e)
{
PersonAttacher.SetPerson(e.Presenter, aPerson);
}
3. Bind to the attached property from within your custom template:
<Style TargetType="lanes:LaneGridHeaderPresenter">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="lanes:LaneGridHeaderPresenter">
<Border BorderBrush="Red" BorderThickness="1">
<TextBlock Text="{Binding Path=(local:PersonAttacher.Person).Name, RelativeSource={RelativeSource TemplatedParent}}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The above template binds to a property Name of the associated person. Note that the binding's relative source is specified to be the templated parent, that is, the LaneGridHeaderPresenter.
I hope this helps.
Regards,
Meppy