TextBlock Background Property removing stretch

流过昼夜 提交于 2019-12-10 20:17:50

问题


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:

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

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


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/14714249/textblock-background-property-removing-stretch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!