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: