Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Setting LaneGrid Header DataContext (Read 2147 times)
binuvc
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 46
Joined: Nov 7th, 2011
Setting LaneGrid Header DataContext
Nov 21st, 2011 at 9:35am
Print Post  
Hi,

We were trying to create the lanegrid header where we need to display a custom object inherited from shapenode to which we would bind a custom model object (say , Person1 with its own properties). The idea was to template the header with our control and set person1 as its datacontext.

It seems that the header is not having a property called datacontext to which we could assign Person1.

Please advice.

Thanks in advance.
  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: Setting LaneGrid Header DataContext
Reply #1 - Nov 21st, 2011 at 11:56am
Print Post  
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.

Code
Select All
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:

Code
Select All
void diagram_LaneGridHeaderCreated(object sender, GridHeaderEventArgs e)
{
      PersonAttacher.SetPerson(e.Presenter, aPerson);
} 


3. Bind to the attached property from within your custom template:

Code
Select All
<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
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint