Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBox with background image and color

Tags:

wpf

textbox

xaml

I have a TextBox control and I would like to be able to set a background image and a background color.

Currently I can set one, or the other but not both. When I try to set both simultaneously I receive a "The property 'Background' is set more than once" error.

Here is the code I used:

<TextBox Name="tbImageTextBox">
      <TextBox.Background>
           <ImageBrush ImageSource="/Resources/Images/image.png" 
                  AlignmentX="Right" Stretch="None"/>
           <SolidColorBrush>#FF8D8A8A</SolidColorBrush>
      </TextBox.Background>
</TextBox>

I have also attempted to set the background color in the style for the TextBox and the image in the <TextBox.Background>, but the color is ignored.

like image 578
binncheol Avatar asked Sep 13 '25 09:09

binncheol


1 Answers

Use the grid resource for background as needed. Same resource can be used for multiple textboxes.

<Grid>
    <Grid.Resources>
        <ImageBrush x:Key="img" ImageSource="Blue hills.jpg"></ImageBrush>
        <SolidColorBrush x:Key="brownBrush" Color="Brown"></SolidColorBrush>
    </Grid.Resources>

    <TextBox x:Name="test" Background="{StaticResource img}" Width="100" Height="40" />
</Grid>
like image 146
Hasan Zubairi Avatar answered Sep 15 '25 15:09

Hasan Zubairi