Blend Behaviors : Expand textbox and add dot dot dot ellipsis

Behaviors is one of the nicest feature in WPF and Silverlight. This article will show how to create and use 2 behaviors :

  • Expand a textbox depending on it’s content
  • Add ‘…’ if the textbox content exceed it’s container size.

Here is a sample video :

There is two behaviors and one helper class in my solution :

And the graphic stack :

 

AddDotDotDotToTextboxBehavior

This class is binded on the ‘…’ textblock and contains a reference on the textbox. This behavior detect on lostfocus if the size overflow in the textbox linked. If it overflows, it displays the textblock.

AugmentSizeOnFocusBehavior

This class is binded on the textbox. This behavior expand and collapse the associated object on lost and get focus events.

TextBoxHelper

This class allows to compute text height depending on the width. It simulate a textblock and compute it’s desired height.

 

Source code

You can download source code at this address : DotDotDot Demo files

Leave a Reply

Your email address will not be published. Required fields are marked *