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