How to set background image to a textbox in wpf application

The following example demonstrates that how to set background image to a textbox in wpf application.

<Window x:Class=”Window1″



    Title=”Window1″ Height=”300″ Width=”500″>


    <TextBox Height=”30″ Width=”237″ MaxLength=”100″

             Name=”tbMultiLine”   >


            <ImageBrush ImageSource=”E:\AuthorCode\Images\s.gif” AlignmentX=”Left” Stretch=”None” />







For this we use imagesource property.


Now if we want to add some more feature in this textbox that if user enter some text in the that textbox then back groud image should be remove otherwise background image will be display(see picture 1 and picture2)



                                            ( Picture 1 )


                                           (picture 2)

Following code are for doing this: Code


    Private Sub tbMultiLine_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles tbMultiLine.TextChanged

        If tbMultiLine.Text = “” Then

            ‘ Create an ImageBrush.

            Dim _ImgBrush As New ImageBrush()


            _ImgBrush.ImageSource = New BitmapImage(New Uri(“E:\AuthorCode\Images\s.gif”))

            _ImgBrush.AlignmentX = AlignmentX.Left

            _ImgBrush.Stretch = Stretch.None


            tbMultiLine.Background = _ImgBrush



            tbMultiLine.Background = Nothing

        End If

    End Sub


C# Code


    private void tbMultiLine_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)


      if (string.IsNullOrEmpty(tbMultiLine.Text)) {

            // Create an ImageBrush.

            ImageBrush _ImgBrush = new ImageBrush();


            _ImgBrush.ImageSource = new BitmapImage(new Uri(“E:\\AuthorCode\\Images\\s.gif”));

            _ImgBrush.AlignmentX = AlignmentX.Left;

            _ImgBrush.Stretch = Stretch.None;


            tbMultiLine.Background = _ImgBrush;


      } else {

            tbMultiLine.Background = null;



One thought on “How to set background image to a textbox in wpf application”

  1. Thank you!
    It’s exactly what I’m looking for.
    MS Visual Studio 2010 WPF designer do not show advanced points for Image object as an background (like AlignmentX)

Comments are closed.