Tip/tip: create and use Silverlight and WPF User Controls

Source: Internet
Author: User

[Original address] tip/TRICK: creating and using Silverlight and WPF User Controls
[Original article publication date] Friday, 1200l 04,200 8 AM

 

A fundamental design goal of Silverlight and WPF is to allow developers to easily encapsulate UI functions into reusable controls.

You can inherit from an existing control class (control base class or control like textbox and button) to implement new custom controls. Alternatively, you can create reusable user controls to easily use the XAML Identity file to form the UI of a control (so it is easy to build it ).

In the sixth part of my Digg tutorial series, I demonstrated how to create a new user control using the "Add new project" dialog box in vs 2008, and then define the UI in it. This method is very effective when you know in advance that you want to encapsulate the UI in the user control. You can also use the same technology in expression blend.

Encapsulate existing UI into user controls

Sometimes, you only know that you want to encapsulate some UI functions into reusable user controls after you have defined some UIS on the parent page or parent control.

For example, we may be designing a form that allows users to enter shipping and checkout information. We may first create some UIS to encapsulate address information. To achieve this goal, we can add a <Border> control to the page, embed a grid layout Panel (two columns and four rows) in it, and place the label and text box controls in it:

After careful layout, we may think, "hi, we should also use the same UI for the checkout address. Maybe we should create a reusable address user control, in this way, we can avoid repeating ourselves ".

We can use the "Add new project" Project template to create a blank new user control and copy the preceding UI content.

But one of the faster tricks that can be used in blend is that in the designer, select the controls we want to encapsulate as user controls and right-click them, select "make control" menu options:

When we select the "make a control" menu item, blend will prompt us to enter the name of the new user control to be created:

We name it "addressusercontrol" and click OK. This causes blend to create a new user control containing the selected content:

After re-compiling the project and returning to the original page, we will see the same UI as before, except that the address UI is now encapsulated into addressusercontrol:

We can name the first addressusercontrol as "shippingaddress ", then add the second instance of the user control on the page to record the checkout information (we name the second control instance "billingaddress "):

Now if we want to change the appearance of the address, we can make changes in only one place, and then we can apply the changes to both shipping and checkout information.

Bind the address object data to addressusercontrol

So far, we have some user controls that encapsulate the address UI. Let's create an address data model class, which can be used to bind data to the user control. We will define a class like the following (using the language features of the new automatic attribute ):

Then in the background of our page. XAML FileCodeFile, we can generate two instances of our address class, one is the shipping address, the other is the checkout address (for the purpose of this routine, we will fill in the pseudo data in the address object ). Then, we will programmatically bind the address object to the addressusercontrol control on the page. We will do this, set the "datacontext" attribute of each user control to an appropriate transport or checkout data template instance:

The last step is to use the declared method in addressusercontrol. add a {binding} statement in the XAML file to configure the text attribute of the Textbox Control in the user control and the bidirectional data binding relationship between the attributes of the address data model object bound to the user control.

When we click F5 to run the application, we will get the automatic data binding between the address data model object and addressusercontrol:

Because we set the {binding} Declaration to "mode = twoway ", changes made in the text box are automatically pushed to the address data model object (which can be implemented without writing any code ).

For example, we can change the original transportation address in the browser to Disney:

If we add a breakpoint to the click event processing function of the Save button, and then click the button, we can see how the changes in the text box above are automatically reflected in our "_ shippingaddress" data model object:

 

Then, we can implement the savebtn_click event processing function, which persists the shipping and checkout address data model objects in our way without manual acquisition or operations on the UI controls on the page.

This clean view/model separation supported by WPF and Silverlight makes it extremely easy to modify the address user control in the future without updating any code on the page, it also makes it easy to perform unit tests on features (please read my latest post to learn more about Silverlight unit tests ).

Conclusion

WPF and Silverlight allow you to encapsulate UI functions into controls. The user control mechanisms supported by these functions provide an easy way. The combination of user controls and data binding facilitates some good view/model separation scenarios, allowing you to write very clean code when operating data.

If you want to run it on your own machine, you can download the complete version of the above routine here.

For more information about Silverlight and WPF, see my Silverlight tutorial and link webpage. I also highly recommend Karen Corby to give a lecture on mix08 (discussing user controls, custom controls, styles, and control templates ), you can watch this lecture online for free.

I hope this article will help you,

SCOTT: http://msdn.microsoft.com/zh-cn/library/dd458703.aspx

 

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.