Thursday, December 25, 2008

Multiline editable combo in WPF

Today I am going to share a interesting learning experience that I had about editable combo box. In one of my project we have a requirement that all editable text need to support  multi-line support. very easy stuff. Just set IsEditable property of combo box to "true".

   1: <ComboBox x:Name="ddlCombo" 

   2: MaxDropDownHeight="200"  

   3: FontWeight="Normal" 

   4: Text="{my:CellEditorBinding}"  

   5: BorderThickness="0" 

   6: Padding="0" Margin="0" 

   7: IsEditable="True"  

   8: Foreground="Black" />

Now, the problem is, in couple of our grid cell we had combo box with editable feature. User can write their own choice or they can pick a value from the drop down list. since the text box of combo is a text editing area. clients wanted warp text with new line in the edit area of the combo box. After few minutes of "R & D" we discovered the way to override the control template and modified the textbox of combo box in such a way so that it can take multi-line and warp text content in combo box.

To give a textbox warped text feature i just added the following code, and it will do the job.

   1: <TextBox x:Name="PART_EditableTextBox"

   2:             Style="{x:Null}" 

   3:             Template="{StaticResource ComboBoxTextBox}" 

   4:             HorizontalAlignment="Left" 

   5:             VerticalAlignment="Center" 

   6:             Margin="3,3,23,3"

   7:             Focusable="True" 

   8:             Background="Transparent"

   9:             Visibility="Hidden"

  10:             TextWrapping="Wrap" 

  11:             AcceptsReturn="True"                                 

  12:             IsReadOnly="{TemplateBinding IsReadOnly}"/>

Best of luck and happy programming.