Monday, August 23, 2010

How to implement TextWrapping in a Silverlight AutoCompleteBox

The Silverlight AutoCompleteBox control is a great control, however, there are some properties that it does not expose like TextWrapping that you might need to access.

The AutoCompleteBox inherits from the TextBox control so accessing the underlying controls properties will allow you to do so.

Simple create a new class file like below which inherits the AutoCompleteBox and on the loaded event gets a reference to the underlying TextBox by calling the GetTemplateChild function of the class.  The GetTempleteChild returns the named element in the visual tree of an instantiated ControlTemplete.  Then simply set the value of the TextBox property.

Public Class clsMyTextBox
    Inherits AutoCompleteBox
    Private _text As TextBox

    Private Sub clsMyTextBox_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        _text = GetTemplateChild("Text")
        _text.TextWrapping = System.Windows.TextWrapping.Wrap

    End Sub
End Class

No comments: