1

If I want to set a image as background on a textBox I can use this code in the axml:

<Grid>
 <Grid.Background>
    <ImageBrush ImageSource="MyImage.jpg" />
 </Grid.Background>
 <TextBlock Text="Some Text" />
</Grid>

However, I am creating a TextBlock in code, I amtrying this:

TextBox myTextBox = new TextBox();

But in this way I don't know how to access to the ImageBrush property.

Which is the way to add a background in code?

Thank so much.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • There is an example in [How to: Add a Watermark to a TextBox](http://msdn.microsoft.com/en-us/library/bb613590.aspx) – Clemens Sep 12 '14 at 09:03

1 Answers1

3

Provided that MyImage.jpg is a file in the application's current folder, you could write

myTextBox.Background = new ImageBrush(new BitmapImage(new Uri("MyImage.jpg")));

If it's a Resource File, you would have to use a Resource File Pack URI:

myTextBox.Background =
    new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/MyImage.jpg")));
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • and I can use a Bitmap instead a URI? For example a memorystream or Bitmap. – Álvaro García Sep 12 '14 at 09:14
  • 1
    BitmapImage can also be created from a Stream. See [here](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.streamsource.aspx) or [here](http://stackoverflow.com/a/13265190/1136211). – Clemens Sep 12 '14 at 09:24