How to change Imagebrush background?

Asked

Viewed 152 times

2

Follows code XAML:

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

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

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

What works is only in the 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));
}

Some solution to change the color ?

1 answer

1


You can set a color to the background of the TextBox using the property TextBox.Background:

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

If you want an image in the background, do it:

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 not for that purpose, it doesn’t make much sense to use it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.