Create a custom behavior and use it in the Silverlight Project

Source: Internet
Author: User

We can writeCodeTo create your own behavior (behavior) and call the custom behavior in expression blend.

We need to inherit from the behavior class. The simplest code for customizing the behavior is as follows:

Dependencyobject is a dependency attribute. For example, it can be Textbox, And Then override the two methods onattached () and ondetaching () of the base class (). We can also include commands in the line.

One way to allow user-defined behavior is to publish commands (Any attribute of the icommand type), where each command corresponds to an operation that behavior can perform. First, make sure that Microsoft. expression. Interactions and system. Windows. Interactivity DLL are referenced in the project.

Modify the above Code as follows:
The following describes how commands and behaviors work together.

Next we will use a specific demo to create our own custom behavior.

Create a Silverlight ApplicationProgram, Named mybehavior,

Before writing behavior, add the reference of interactivity.


Then we repeat the onattached () and ondetaching () methods.


Enter associatedobject, which is a behavior-related object. It has some attributes and events.


Here we write some fields, such:


Then write the onattached event and register the keydown, gotfocus, and lostfocus events.



Then we can call the newly created behavior in our SL application.

First, you must add the reason in the XAML code, as shown in figure


Other XAML code is as follows:


Finally, run the program. The effect is as follows:


Download Code:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.