vikke
vikke

Reputation: 127

Add behaviour to TextBlock silverlight 3

I want to add a behaviour to a TextBlock in silverlight 3.

I have a behaviour class in a c# file in a different project than my xaml file within my solution.

public class FooBehavior : Behavior<TextBlock>
{
   ...
}

How do I attach this behaviour to my TextBlock? Would be nice to do without involving c# code.

Upvotes: 2

Views: 629

Answers (2)

Bryant
Bryant

Reputation: 8670

ChrisF has the correct answer for how to write the Xaml to add the behavior. However, if you have Blend it is even simpler.

  1. Open your project in Blend
  2. On the tools toolbar click the >> button
  3. Click on Behaviors
  4. Find your Behavior and Drag it over your TextBlock and drop it

Blend will add all the proper namespaces for you.

Upvotes: 1

ChrisF
ChrisF

Reputation: 137128

Include the following lines in the definition of your UserControl:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:myBehaviors="clr-namespace:MyNamespace.Behaviors;assembly=MyAssembly"

Then on the TextBlock have this code:

<TextBlock .....>
    <i:Interaction.Behaviors>
        <myBehaviors:FooBehaviour/>
    </i:Interaction.Behaviors>
</TextBlock>

Upvotes: 4

Related Questions