Custom Controls for WPF control Development (2)

Source: Internet
Author: User

The metadata of the property.Metadata includes the default values of this attribute, coercevaluecallback, and propertychangedcallback.

The get and set accessors that implement this attribute define a CLR "Wrap" attribute named value, which is used to register the name of this dependency attribute. Note that the get and set accessors only call getvalue and setvalue respectively. It is recommended that the accessors of the dependency attribute do not include other logic, because the client and WPF can bypass these two accessors to directly call getvalue and setvalue. For example, if a property is bound to a data source, the set accessors of the property are not called. Instead of adding other logic to the get and set accessors, use validatevaluecallback, coercevaluecallback, and propertychangedcallback to delegate or check this value when the value is changed.

Define a method named coercevalue for coercevaluecallback. Coercevalue ensures that value is greater than or equal to minvalue and less than or equal to maxvalue.

Define a method named onvaluechanged for propertychangedcallback. Onvaluechanged creates a routedpropertychangedeventargs <(of <(T>)> object and prepares to raise the valuechanged routing event. Routing events are discussed in the next section.

Use routedevent
Just as the Dependency Property expands the purpose of the CLR property with additional features, the routing event extends the purpose of the Standard CLR event. When creating a new WPF control, implementing the event as a routing event is also a good way, because the routing event supports the following actions:

Events can be processed on the parent of multiple controls. If an event is a bubble event, a single parent in the element tree can reserve the event. Then, the application Author can use a handler to respond to the event of multiple controls. For example, if the control belongs to each item in ListBox (because it is contained in datatemplate), application developers can define corresponding Event Handlers for The ListBox event of the control. This event handler is called whenever any control in these controls has this event.

Routing events can be used in eventsetter. application developers can use eventsetter to specify the event handler in the style.

Routing events can be used in eventtrigger, which is useful for animation processing of attributes using XAML.

The following example defines a routing event.

 

This code executes the following operations:

Define a routedevent identifier named valuechangedevent as the publicstaticreadonly field.

You can call eventmanager...:. registerroutedevent to register a route event. This example specifies the following information when calling registerroutedevent:

The event name is valuechanged.

The routing policy is bubble, which means that the event handler on the source (the object that triggers the event) is called first, and then the event handler on the nearest parent element starts, call the event handlers on each parent element of the source one after another.

The event handler is of the routedpropertychangedeventhandler <(of <(T>)> type and is constructed using the decimal type.

The event belongs to the numericupdown type.

Declare a public event named valuechanged and include the event accessors declaration. This example calls addhandler in the add accessors declaration and removehandler in the remove accessors declaration to use the WPF event service.

Create a protected virtual method named onvaluechanged, which triggers the valuechanged event.

Use binding
To separate the control UI from its logic, consider using data binding. This is especially important if controltemplate is used to define the appearance of the control. When using data binding, you may avoid referencing specific parts of the UI.

The following example updates the textblock of the numericupdown control, assigns it a name, and then references the text box by name in the code.

 

 

The following example uses binding to achieve the same purpose.

Designed for the designer
To support custom WPF controls in the Visual Studio Windows Presentation Foundation (WPF) designer (for example, using the "properties" window to edit properties), follow these guidelines.

Dependency Property
Ensure that the clrget accessors and set accessors are implemented, as described in "using dependency properties. The designer can use packaging to check whether a dependency property exists. However, like WPF and the control client, the designer is not required to call the accessors when obtaining or setting properties.

Additional attributes
Additional properties on custom controls should be implemented in accordance with the following principles:

A publicstaticreadonlydependencyproperty created using the registerattached method is available in the form of "property name property ". The property name passed to registerattached must match the property name.

Implement a public static CLR method named set property name and get property name. Both methods should accept the class derived from dependencyproperty as its first parameter. The Set property name method also accepts parameters whose type matches the registered data type of the property. The get attribute name method should return values of the same type. If the set attribute name method is missing, the attribute is marked as read-only.

The Set property name and get property name must be directly routed to the getvalue and setvalue methods of the target dependency object. By calling method packaging or directly calling the target dependency object, the designer can access additional attributes.

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.