WPF and Expression Blend development examples: Adorner (adorner) application examples

Source: Internet
Author: User

Adorner--

an abstract class that represents the FrameworkElement used to decorate UIElement

To put it simply, a visual object is added to it without changing a uielement structure.

Application Examples:

Now we have a text box, but we need to qualify the input string, and when the input is an illegal string, a red border is required around the text box. Usually we can wrap the text box inside with border, And then dynamically change its color to implement the function. Now we can add an adorner directly to the text box.

Adorner class

Adornerlayer class

The adorner is placed inside the decorative layer (adornerlayer), which means we can add multiple adorners to the UIElement. Getadornerlayer by Adornerlayer static method, We can easily get a UIElement decorative layer.

Next we just inherit the abstract class of adorner, to implement a really usable adorner, and then add the adorner to the decorative layer on the control.

 Public classadornerfortextbox:adorner{Privatevisualcollection Visual; PrivateBorder Border;  PublicAdornerfortextbox (UIElement UIElement):Base(uiElement) {visual=NewVisualCollection ( This); #regionDefine what the adorner looks likeBorder=NewBorder (); Border. BorderBrush=brushes.red; Border. BorderThickness=NewThickness (1); Border. Height= -; Border. Width= the; #endregionVisual.    ADD (border); }    protected Override intVisualchildrencount {Get        {            returnVisual.        Count; }    }    protected OverrideVisual Getvisualchild (intindex) {        returnVisual[index]; }    protected Overridesize measureoverride (size constraint) {return Base.    MeasureOverride (constraint); }    protected Overridesize arrangeoverride (size finalsize) {border. Arrange (NewRect (finalsize)); Border. Margin=NewThickness (0,0,0,0);//draw the adorner's final position on the element        return Base.    Arrangeoverride (finalsize); }
}

Display Adorner

varAdornerlayer =Adornerlayer.getadornerlayer (tb_adorner);if(Adornerlayer! =NULL){    //empty the original adorner    varadorners =adornerlayer.getadorners (Tb_adorner); if(adorners! =NULL&& adorners. Count () >0)    {         for(inti =0; I < adorners. Count (); i++) {adornerlayer.remove (adorners[i]); }} adornerlayer.add (NewAdornerfortextbox (Tb_adorner));}

:

Source code Download:

Http://files.cnblogs.com/youngytj/AdornerTest.rar

WPF and Expression Blend development examples: Adorner (adorner) application examples

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.