2

Again am stuck to change the background color of my text box but the weird part is forground is works fine but not the background.

Here is my xaml

 <TextBox
                Name="tbHeadline" 
                Text="{Binding SelectedStory.Headline, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" 
                Grid.Column="1"  
                Grid.Row="6" 
                TextWrapping="NoWrap" 
                d:LayoutOverrides="Height" 
                Grid.ColumnSpan="2" 
                HorizontalAlignment="Stretch" 
                LostFocus="tbHeadline_LostFocus" 
                 />

in my Xaml.cs

I have the following code

 tbHeadline.Background = Brushes.Gray; //this not working
 tbHeadline.Foreground = Brushes.Gray; //this is working

here is the sample output

Foreground output Background Output Thanks for your help.

Update from the expert comments

Okie, i added a textbox in the grid and this what it look like

<TextBox Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="10" Grid.RowSpan="2" Height="23" HorizontalAlignment="Left" Margin="50,14,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Background="#E6000000" />

Even i set the background color to Black but its not visible when i run the application.

9
  • this should be working fine. try to delete the textbox, and create new textbox and try to rebuild. Commented May 21, 2013 at 1:05
  • 1
    Is your error template overriding the background? Commented May 21, 2013 at 1:13
  • @MarkHall,that's my doubt too. Commented May 21, 2013 at 1:15
  • Try removing the error template reference from your xaml then see if the issue goes away, then look at your errorhandler's xaml to verify. Commented May 21, 2013 at 1:19
  • I tried remove the errorhandler but no luck Commented May 21, 2013 at 2:30

4 Answers 4

3

In summarizing our discussion in Chat, you have a Default Style that is overriding your TextBox's Background Brush. I suggested that you set your TextBox's Default Style to Null as the answer to this SO question suggests. You can do this in either the Code Behind or in the Xaml declaration of your TextBox.

tbHeadline.Style = null;

or in your Xaml

<TextBox Name="tbHeadline" Style="" 
         Text="{Binding SelectedStory.Headline, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" 
         Validation.ErrorTemplate="{StaticResource ErrorTemplate}" 
         Grid.Column="1"  
         Grid.Row="6" 
         TextWrapping="NoWrap" 
         d:LayoutOverrides="Height" 
         Grid.ColumnSpan="2" 
         HorizontalAlignment="Stretch" 
         LostFocus="tbHeadline_LostFocus" />
Sign up to request clarification or add additional context in comments.

Comments

3

Your code snippet should work. You can test by creating a blank project, dropping a TextBox and setting the colors in code. Use Snoop to see if tb's background is being set in different ways or different places.

Comments

2

Your code snippet should work. Just set style to null like this

<TextBox Style ={x:Null} Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="10" Grid.RowSpan="2"         Height="23" HorizontalAlignment="Left" Margin="50,14,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Background="#E6000000" />

Comments

0

Try tbHeadline.Background = new SolidColorBrush(Colors.Gray);

3 Comments

Thanks inxs,i already tried that option like tbHeadline.Background = new SolidColorBrush(Colors.Red); but nothing happend
my style is overwrite it or i can say the background color is inherited from sytle.
Background needs a Object of the Type Brush like SolidColorBrush, GradientBrush etc. See here for more information: msdn.microsoft.com/de-de/library/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.