3

I have a TextBox defined as this:

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

It looks like this:

enter image description here

However, when I remove the Background property, the text stretches like this:

enter image description here

Is there any way to add the background without changing the way the text looks?

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Duke Cyrillus
  • 1,217
  • 2
  • 14
  • 29

2 Answers2

1

If you use Background="Transparent" it will use the same layout but with no background color. Is that what you're trying to do?

John Bowen
  • 24,213
  • 4
  • 58
  • 56
  • No, in the second image the text seems to fill the background, while in the first image it seems to have a padding of some sort. I want to retain the background colour while having the text sttretch to fill the area. – Duke Cyrillus Feb 05 '13 at 18:38
0

a workarround of this problem which i don't know why it occurs would be to remove Background property from textblock and put it behind it like this

        <Grid>
            <Rectangle Fill="Blue"/>
            <TextBox Height="100">
                <TextBox.Background>
                    <VisualBrush Stretch="Fill" TileMode="None" AlignmentX="Left" AlignmentY="Top">
                        <VisualBrush.Visual>
                            <StackPanel>
                                <TextBlock Margin="0" Padding="0" Opacity="0.5" Text="155"/>
                            </StackPanel>

                        </VisualBrush.Visual>
                    </VisualBrush>
                </TextBox.Background>
            </TextBox>
        </Grid>
iltzortz
  • 2,342
  • 2
  • 19
  • 35