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 atta开发者_如何学运维ch this behaviour to my TextBlock? Would be nice to do without involving c# code.
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>
ChrisF has the correct answer for how to write the Xaml to add the behavior. However, if you have Blend it is even simpler.
- Open your project in Blend
- On the tools toolbar click the >> button
- Click on Behaviors
- Find your Behavior and Drag it over your TextBlock and drop it
Blend will add all the proper namespaces for you.
精彩评论