3

Coming off of this question, I have a textbox defined as this:

<TextBox>
    <TextBox.Background>
        <VisualBrush Stretch="Uniform">
            <VisualBrush.Visual>
                <StackPanel>
                    <TextBlock Background="Blue" Opacity="0.5" Text="155"/>
                </StackPanel>
            </VisualBrush.Visual>
        </VisualBrush>
    </TextBox.Background>
</TextBox>

This results in a TextBox like this:

TextBox with Background

Now if I remove the background property, the TextBoxlooks like this:

TextBox without background

What I want is to achieve the second image with a colored background. In the first image for example, I want the background colour to fill the remaining whitespaces as well.

Community
  • 1
  • 1
Duke Cyrillus
  • 1,217
  • 2
  • 14
  • 29

1 Answers1

1

You can achieve this by adding Grid with background also as VisualBrush and in that grid you can add your TextBox:

<Grid>
    <Grid.Style>
        <Style TargetType="Grid">
            <Setter Property="Background">
                <Setter.Value>
                    <VisualBrush Stretch="Fill">
                        <VisualBrush.Visual>
                            <Rectangle Stretch="Fill" Stroke="Blue" Opacity="0.5" />
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Style>
    <TextBox>
        <TextBox.Style>               
            <Style TargetType="TextBox">
                <Setter Property="Foreground" Value="Black" />                    
                <Setter Property="Background">
                    <Setter.Value>
                        <VisualBrush Stretch="Uniform">
                            <VisualBrush.Visual>
                                <TextBlock Foreground="Gray" Opacity="0.5" Text="155"/>
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Setter.Value>
                </Setter>
            </Style>
        </TextBox.Style>
    </TextBox>
</Grid>
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
  • Doing this is going to make everything, including anything typed into the TextBox, semi-transparent. – John Bowen Feb 06 '13 at 19:26
  • @JohnBowen check my answer again. I solved that problem by using `VisualBrush` in grid. – kmatyaszek Feb 06 '13 at 19:46
  • Rather than adding all that complexity you could just do Background="#800000FF" on the Grid. – John Bowen Feb 06 '13 at 19:56
  • Yes it's complexity but if we will change color in the future we can write `Stroke="Red"` in `Rectangle`. We don't have write hexadecimal color value. – kmatyaszek Feb 06 '13 at 20:04
  • This is good. However, I need the background to somehow be part of the VisualBrush/StackPanel/TextBlock but nothing outside it since this TextBox is supposed to be a custom control and I need to be able to change the background colour based on certain conditions. – Duke Cyrillus Feb 06 '13 at 20:16
  • If you don't want to write hex colors it's still much easier and more performant to write the expansion of the brush: – John Bowen Feb 06 '13 at 20:16
  • @DukeCyrillus you can do this with my solution. What conditions do you have? – kmatyaszek Feb 07 '13 at 16:34
  • @kmatyaszek The TextBox is actually a custom control inheriting TextBox. Its background is a TextBlock that displays the character count and currently the count changes colour when a certain number of characters remain. However, I want the background to change color as well and the logic to change the color should reside in the code for the custom control, which is why I wanted it to be the TextBlock Background – Duke Cyrillus Feb 07 '13 at 19:49