Blend Behaviors : Expand textbox and add dot dot dot ellipsis

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 :
[jwplayer config=”GrundgeAdaptSmall” file=”http://www.alphablog.org/TiercePartie/Video/BehaviorSample.mp4″]
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

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *