TextBox.Background property in VisualBrush behaves strange

Disabling this question, I have a textbox defined like 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>

      

The result is TextBox

:

TextBox with Background

Now if I remove the background property TextBox

it looks like this:

TextBox without background

I want to achieve a second image with a colored background. For example, in the first image, I want the background color to fill the remaining gaps as well.

+3


source to share


1 answer


You can achieve this by adding Grid

with the background as well VisualBrush

, and in this grid you can add yours 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>

      

+1


source







All Articles