5

Semi-Transparent background of the Textbox is needed, and the text content should be shown as normal.

Style or Brush which can store in the Resource dictionary is good.

NOTE:

  1. My textBox is wrapped within a ContentControl.

  2. This similar question does not help. TextBox with a Transparent Background .

Community
  • 1
  • 1
dongx
  • 1,722
  • 4
  • 16
  • 36

2 Answers2

17

In XAML you can set Background property to Transparent:

<TextBox Background="Transparent" />

In code-behind you can use following code:

TextBox tb = new TextBox 
{
    Width = 100,
    Background = Brushes.Transparent
};

If you want to set background to transparent to all TextBox you can use following style:

<Style TargetType="TextBox">
    <Setter Property="Background" Value="Transparent" />
</Style>
kmatyaszek
  • 18,648
  • 9
  • 57
  • 63
  • It works for a single Textbox, but for some reasons not work for my Textbox wrapped within a ContentControl. Thanks anyway. – dongx Feb 20 '13 at 17:37
  • @dongx if you show me more code i will try find out why it's not working. I tried `TextBox` wrapped within a `ContentControl` and it's working in my code. – kmatyaszek Feb 20 '13 at 17:42
  • Do you know how to set the background to be semi-transparent? – dongx Feb 20 '13 at 18:33
  • Finally, it works. The reason is the textbox's style has been overwrote. – dongx Feb 20 '13 at 18:43
1

If you want to set a semi-transparent background in code-behind you can do this

use a dependency prop on a class that inherits from TextBox

public static readonly DependencyProperty BgColourProperty =
  DependencyProperty.Register("BgColour", typeof(SolidColorBrush), typeof(myTextBox), null);

public SolidColorBrush BgColour
    {
        get { return (SolidColorBrush)GetValue(BgColourProperty); }
        set { SetValue(BgColourProperty, value); }
    }

then set whatever colour you wish using Color.FromArgb() where 1st argument is Alpha component

myTextBox.BgColour = new SolidColorBrush(Color.FromArgb(120,240, 17, 17));
Constanta
  • 399
  • 4
  • 15