VisualBrush画刷是一种特殊的画刷,他通过将一定区域映射为某一空间的外观。
重要属性:Visual:设置映射源。
实例练习如下:
<Grid>
<TextBox Height="44" HorizontalAlignment="Left" Margin="112,75,0,0" Name="textBox1" VerticalAlignment="Top" Width="250" FontSize="16" />
<Rectangle Height="45" HorizontalAlignment="Left" Margin="112,148,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="0" VerticalAlignment="Top" Width="250" >
<Rectangle.Fill>
<VisualBrush Visual="{Binding ElementName=textBox1}">
<VisualBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="-1"/>
<TranslateTransform Y="1"/>
</TransformGroup>
</VisualBrush.RelativeTransform>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
Visual=”{Binding ElementName=textBox1}”:指将textBox1作为映射源.
VisualBrush.RelativeTransform:指对图像进行相对变换。
ScaleTransform:指对图像进行缩放。ScaleX沿X轴进行变换,ScaleY沿Y轴进行变化。
如果ScaleX的值介于0-1则对图像进行缩小变化,大于1则对对象进行放大。负数指对图像进行缩放并翻转,0-(-1)指对图像进行缩小并翻转.
Y表示对图像沿Y轴移动的距离,默认值为0。正值表示向下移动对象,负值表示向上移动对象。
TranslateTransform:对图像进行位置偏移,Y:相对Y轴的偏移量。
由于ScaleTransform和TranslateTransform都是对图像进行转换,所以将它们放在一个TransformGroup内。
练习2如下:
<Grid Height="317" Width="515">
<Grid.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBox Height="55" HorizontalAlignment="Left" Margin="70,21,0,0" Name="textBox1" VerticalAlignment="Top" Width="345" FontSize="20" FontStyle="Normal" FontWeight="Bold">
<TextBox.Background>
<ImageBrush ImageSource="images/1.jpg"/>
</TextBox.Background>
</TextBox>
<Rectangle Height="110" HorizontalAlignment="Left" Margin="70,82,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="0" VerticalAlignment="Top" Width="345" >
<Rectangle.Fill>
<VisualBrush Visual="{Binding ElementName=textBox1}">
<VisualBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="-1"/>
<TranslateTransform Y="1"/>
</TransformGroup>
</VisualBrush.RelativeTransform>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>