How to change background of ImageBrush?

2

XAML:

<TextBox />
    <TextBox.Background>
        <ImageBrush x:Name="image_background" Stretch="Fill" TileMode="None" AlignmentX="Left" AlignmentY="Center" />
    </TextBox.Background>
</TextBox>

C #: (The following code has an event click , to change the image of the black color textbox)

image_background.ImageSource = CreateBitmapSource(System.Windows.Media.Color.FromArgb(255, 0, 0, 0)); // Black Color

What works is only in event Loaded :

private void Window_Loaded(object sender, RoutedEventArgs e)
{
   // aqui consigo mudar a cor
   image_background.ImageSource = CreateBitmapSource(System.Windows.Media.Color.FromArgb(255, 0, 255, 0));
}

Any solution to change color?

    
asked by anonymous 30.12.2017 / 03:04

1 answer

1

You can set a color for the TextBox background by using the TextBox.Background :

textBox.Background = Brushes.Black; // a classe Brushes já tem cores pré definidas

If you want an image on the background, do:

var imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri("caminho/arquivo.jpg", UriKind.Relative));
textBox.Background = imageBrush;

The ImageBrush is to fill an area with a image . If it is not for this purpose, it does not make much sense to use it.

    
30.12.2017 / 03:22