开发者

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.

  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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜