我想缩放文本框的背景图像,但它被裁剪
问题描述:
我有一个TextBox,我想在其中添加一个图像作为背景。第一次尝试是这样的:我想缩放文本框的背景图像,但它被裁剪
<TextBox Text="{Binding Titulo}" Height="Auto" HorizontalAlignment="Stretch" Margin="5,5,5,5" Name="txtTitulo" VerticalAlignment="Stretch"
TextAlignment="Center"
FontSize="30"
Padding="0,20,0,0"
FontWeight="Heavy"
BorderBrush="red"
BorderThickness="5">
<TextBox.Background>
<ImageBrush ImageSource="../Imagenes/Logo (233x251) - fondo transparente (test).png" AlignmentX="Center" Stretch="None" AlignmentY="Center">
</ImageBrush>
</TextBox.Background>
</TextBox>
在这种情况下,结果是这样的:
如果我使用此代码,使用变换:
<TextBox Text="{Binding Titulo}" Height="Auto" HorizontalAlignment="Stretch" Margin="5,5,5,5" Name="txtTitulo" VerticalAlignment="Stretch"
TextAlignment="Center"
FontSize="30"
Padding="0,20,0,0"
FontWeight="Heavy"
BorderBrush="red"
BorderThickness="5">
<TextBox.Background>
<ImageBrush ImageSource="../Imagenes/Logo (233x251) - fondo transparente (test).png" AlignmentX="Center" Stretch="None" AlignmentY="Center">
<ImageBrush.Transform>
<ScaleTransform ScaleX="0.5" ScaleY="0.5" CenterX="0" CenterY="0" />
</ImageBrush.Transform>
</ImageBrush>
</TextBox.Background>
</TextBox>
结果这个:
,我想缩放图像是这样的:
看来,当它扩展,缩小尺寸,但它的图像裁剪到文本框的大小。
我想要做的是缩放图像看到完整的图像,但我无法得到它。
答
您需要使用Uniform
作为Stretch
属性的值。就像这样:
Stretch="Uniform"
所以,你的代码应该是这样的:
<TextBox Text="{Binding Titulo}" Height="Auto" HorizontalAlignment="Stretch" Margin="5,5,5,5" Name="txtTitulo" VerticalAlignment="Stretch"
TextAlignment="Center"
FontSize="30"
Padding="0,20,0,0"
FontWeight="Heavy"
BorderBrush="red"
BorderThickness="5">
<TextBox.Background>
<ImageBrush ImageSource="../Imagenes/Logo (233x251) - fondo transparente (test).png" AlignmentX="Center" Stretch="Uniform" AlignmentY="Center"/>
</TextBox.Background>
</TextBox>
的Uniform
值使图像填补所有可用空间,同时保持其原有的规模。
作为一个方面说明,因为您正在使用'None'作为'Stretch'属性的值,所以您的图像保持其原始的固定大小。因此,如果您要缩放文本框,则图像不会一起缩放,因为它具有固定的大小。 –
通过再次阅读您的问题,我不确定你想要完成什么。你想让图像适合在文本框内(文本框内的行星图像),还是想让图像显示出文本框界限(行星图像内的文本框)? 我上面发布的解决方案使图像适合文本框内(行星图像保持在文本框内)。 –