WPF control application [2]

Source: Internet
Author: User

Source: MSDN

Custom button Behavior

At this time, you have customized the button representation by editing the button template, but the button does not respond to user operations as a typical button (for example, hovering over the mouse and receiving the Focus)
And change the appearance when you click ). The following two processes describe how to generate a behavior similar to this to a custom button. We will start with a simple property trigger, and then add the event trigger
And animation.

Set property triggers

1. Create a new property trigger: Select "glassCube" and click "+ property" in the "Trigger" panel"
2. (see the figure below next ). In this way, you can create a default property trigger.
Set IsMouseOver to the attribute used by the trigger: change this attribute to IsMouseOver. This will activate this property trigger when the IsMouseOver attribute is true (that is, when the user points the mouse to the button.

3. IsMouseOver: 100% opacity triggered by glassCube: you will see "Trigger record function is enabled" [Trigger recording is on]
(SEE ). This means that when the record function is enabled, any changes made to the "glassCube" attribute value will be performed when IsMouseOver is set to true.
. During the record period, "glassCube" now, you have created the first property trigger. Note that the Opacity of the editor is changed to 100%.

Now you have created the first property trigger. Note that the "Trigger" Panel of the editor records that the Opacity has been changed to 100%.

Press F5 to run the application, move the mouse pointer to the button, and then remove it. You should see that the glass layer is displayed when the mouse moves over the button, and the glass layer disappears when the pointer leaves.

4. IsMouseOver triggers stroke value change: some other operations are associated with the IsMouseOver trigger. When record continues, switch the selected content from glassCube to outerRectangle ". Then, set the Stroke of "outerRectangle" to the custom expression "{DynamicResource {x: Static SystemColors. HighlightBrushKey }}". This sets the Stroke to the typical highlight color used by the button. Press F5 to view the effect of moving the mouse over the button.

5. IsMouseOver: Associate another operation with the IsMouseOver attribute trigger. Display the button content when the glass layer is displayed on the button
Fuzzy. Therefore, you can apply the fuzzy BitmapEffect to ContentPresenter (myContentPresenter ).

Note: To return the appearance before searching BitmapEffect from the properties panel, clear the text in the search box.

At this time, we have used attribute triggers for several associated operations to create a highlighted behavior when the mouse pointer enters and leaves the button area. Another typical behavior of buttons is
Is highlighted when there is focus (I .e. when clicked. You can add another property trigger for the IsFocused attribute.

6. Create an attribute trigger for IsFocused: use the same process as IsMouseOver (see step 1 in this section) to create another attribute for IsFocused
Trigger. When "Trigger record function is enabled" [Trigger recording is on] is displayed, add the following operations to the Trigger:
• Get 100% Opacity for glassCube.
• "OuterRectangle" gets the Stroke custom expression value "{DynamicResource {x: Static SystemColors. HighlightBrushKey }}".
As the last step of this walkthrough, we will add an animation to the button. These animations are triggered by events (specifically, MouseEnter and Click events.
Use event triggers and animations to increase activity appeal

1. Create a MouseEnter event trigger: Add a new event trigger and select MouseEnter as the event to be used in the trigger.

2. Create an animation timeline. Next, associate the animation timeline with the MouseEnter event.

After you press "OK" to create a new Timeline, the "Timeline" panel is displayed, and the "Timeline record function is enabled" [Timeline recording is on] is displayed on the design panel. This means that you can begin to record property changes in the timeline (animations of property changes ).

Note: you may need to adjust the window and/or panel size to see the display effect.

3. create a key frame: To create an animation, select the object for which you want to set the animation, and create two or more key frames on the time line, set the animation attribute values for these key frames. It will guide you through the key frame creation process.

4. Reduce glassCube at this key frame: After selecting the second key frame, use "Size conversion" [Size Transform] to reduce the Size of "glassCube" to 90% of its actual Size.

Press F5 to run the application. Move the cursor over the button. You will see that the glass layer on this button is reduced.

5. Create another event trigger and associate it with another Animation: Next, add another animation. This process is similar to the process used to create an event trigger Animation:
A. Use the Click event to create a new event trigger.
B. associate a new timeline with the Click event.

C. Create two key frames in the time line, one in 0.0 seconds and the other in 0.3 seconds.
D. After highlighting the key frame at 0.3 seconds, set "rotation conversion Angle" [Rotate Transform Angle] to 360 degrees.

Conclusion

You have completed the Custom button process. This process is implemented using a button template, which applies to all buttons in the application.

 

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: info-contact@alibabacloud.com 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.