WPF: the control attributes do not have frameworkpropertymetadataoptions. affectsrender and onrender.

Source: Internet
Author: User

The title is the fact. The dependency attribute that affects display in the control type is not set at registration, but the frameworkpropertymetadataoptions. affectsrender attribute is frameworkpropertymetadataoptions. None, such as foreground, background, borderthickness ....... The other text-related attributes are frameworkpropertymetadataoptions. inherits, which also have no affectsrender enumeration values.

You can easily see these in reflector:

 

Why? The reason is very simple. In WPF, the control's external definition is based on the control template, and the control template usually uses the data binding of XAML to display the value of the dependency attribute, therefore, the value after the value of the source dependency attribute changes will be immediately fed back to the interface by data binding.

 

However, an issue occurs when you use onrender to display your own controls. For example, you can define a custom type:

Class testcontrol: Control

{

Protected override size arrangeoverride (size arrangebounds)

{

Return new size (100,100 );

}

 

Protected override void onrender (drawingcontext)

{

Base. onrender (drawingcontext );

 

Drawingcontext. drawrectangle (background, null, new rect (new point (), rendersize ));

}

}

 

Modify the attributes of testcontrol in another place, such as background:

// The test variable is an instance of testcontrol.

Test. Background = brushes. Green;

 

The result is that the background of testcontrol on the interface will not change. At this time, no matter whether you call invalidatevisual, updatelayout, or even the invalidatemeasure that completely re-runs the layout process, the interface will not respond, the reason is that the dispatcher operation queue of the control does not have any waiting logic and the layout size of the control has not changed. Therefore, the WPF Drawing Engine considers that the control does not need to be updated, so the onrender will not be called.

 

The solution is to add the affectsrender enumeration value for the corresponding dependency attribute, and rewrite the metadata of the dependency attribute through the addowner or overridemetadata of dependencyproperty:

Static testcontrol ()

{

Backgroundproperty. overridemetadata (typeof (testcontrol), new frameworkpropertymetadata (brushes. Red, frameworkpropertymetadataoptions. affectsrender ));

}

 

The problem is solved. When the background attribute is set, onrender is automatically called by WPF and the background color is updated immediately.

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.