Question

I'm trying to implement a behavior that works on an object, but is affected by a second object.

As an example, it will change the appearance of the framework element based on the distance of another framework element.

I know just enough about attached behaviors to be dangerous, and I would define my class like this :

 public class ChangeWithDistance : Behavior<FrameworkElement>

And somewhere in the behavior I would need a dependency property that would point at the name(?) of the other control.

What is the class I should make my dependency property so that I can have this behavior point to another control?

Would I specify the propdp as a string that represents the name of the other control?

Or is there another class I should use that signifies the tag associated with a control (you know, that x:Name business..)

Or should the propdp be pointing at a framework element like this?

    public static readonly DependencyProperty OtherControl =     
          DependencyProperty.Register("OtherControl", typeof(FrameworkElement)

Or do I do something with binding?

Is there a best practice of how to point a behavior or really anything that has a dependency property at another control?

Is there a way to make it show up so a user using blend can select the other control?

Was it helpful?

Solution

I'm having good luck declaring my dependency property as such :

 public static readonly DependencyProperty OtherObjectProperty =
        DependencyProperty.Register("OtherObject", typeof (FrameworkElement), typeof (MyBehavior), new UIPropertyMetadata());

And then in the XAML binding it to the other object I am interested like this :

  <Rectangle ....>
       <Interactivity:Interaction.Behaviors>
             <Controls:MyBehavior OtherObject="{Binding ElementName=TheOtherElementNameIWantToReference}" />
       </Interactivity:Interaction.Behaviors>
  </Rectangle>                   

OTHER TIPS

That's a lot of questions in one. Read up on attached properties: http://msdn.microsoft.com/en-us/library/ms749011.aspx

The short answer is that you need to use the RegisterAttached method, rather than Register.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top