Aligoglos
Aligoglos

Reputation: 93

how to set image in textbox in wpf

I have a TextBox in my application that I want to add an image to.

In some cases I'd like both an image and text.

Is it possible and how can this be done?

Thanks.

Upvotes: 3

Views: 9635

Answers (1)

ChrisF
ChrisF

Reputation: 137128

From XAML:

<TextBox Name="myTextBox" TextChanged="OnTextBoxTextChanged" Width="200">
  <TextBox.Background>
    <ImageBrush ImageSource="TextBoxBackground.gif" AlignmentX="Left" Stretch="None" />
  </TextBox.Background>
</TextBox>

From C#

// Create an ImageBrush.
ImageBrush textImageBrush = new ImageBrush();
textImageBrush.ImageSource = new BitmapImage(new Uri(@"TextBoxBackground.gif", UriKind.Relative));
textImageBrush.AlignmentX = AlignmentX.Left;
textImageBrush.Stretch = Stretch.None;
// Use the brush to paint the button's background.
myTextBox.Background = textImageBrush;

Source

Upvotes: 6

Related Questions