AdornerDecorator Example

boxingadorner.jpg

I believe a Window automatically contains a Decorator, and you could actually create more than one Decorator. However, in general, you will have one AdornerDecorator which you get for free in CPSBaseWindow, and which you may think of as merely a specialized, transparent Canvas with a high z-order. In WPF-CPS, this AdornerDecorator is itself an IParent that you may add children to.

Typically, an AdornerDecorator is a layer at a high z-index that you may use to "adorn" things on top of Framework Elements, such as text or a rectangle. I provide examples for adorning Text Boxes with red rectangles for the purposes of identifying invalid required fields when performing field validation.

Display a Red Boxing Adorner on a TextBox When Performing Field Validation

When performing field validation, a rudimentary way to highlight invalid required fields is to add a red BoxingAdorner around each of them. This can be done via the AdornerDecoratorExt.DisplayErrorRectangle method. In the example below, if the TextBox at X-Y position 2-1 is blank, then a red error rectangle will be displayed on it.

TextBox txtBox = thisComposite.GetChildFromComposite<TextBox, Grid>(2, 1);
if (txtBox.Text == null || (txtBox.Text == String.Empty))
{
      AdornerDecoratorExt.DisplayErrorRectangle(txtBox);
}
else
{
      AdornerDecoratorExt.RemoveErrorRectangle(txtBox);
}

In rare cases (such as when using the ContentControl3D control), the AdornerLayer.GetAdornerLayer method may return null.

Normally, the AdornerDecorator that I add in my examples (or the built-in one within Window) is readily returned by GetAdornerLayer. However, just in case, I do allow you to pass in your own AdornerLayer explicitly. If you do so, the AdornerLayer may work incorrectly, positioning the BoxingAdorner wrong. To adjust this, you may pass in both X-Y position offsets AND width/height offsets. For example:

ComboBox cbBox = compositeToValidate.GetChildFromComposite<ComboBox, ContentControl3D>(3, 1);
if (cbBox.Text == null || (cbBox.Text == String.Empty))
{             
AdornerDecoratorExt.DisplayErrorRectangle(cbBox , AdornerLayer.GetAdornerLayer(this), 43D, -1D, 34D, -1D);
}
else
{              
AdornerDecoratorExt.RemoveErrorRectangle(cbBox , AdornerLayer.GetAdornerLayer(this), 43D, -1D, 34D, -1D);
tempResult=true;
}

Last edited Apr 2, 2014 at 5:57 PM by stagathome0069, version 12

Comments

No comments yet.