调整大小时防止XAML网格背景中的变形

时间:2009-06-23 01:56:14

标签: wpf xaml brushes

我正在XAML中编写一个画笔,我可以用它来绘制Grid的背景来创建横幅。它看起来像这样:

An example of the brush applied to the background of a Grid

Grid调整大小时,我希望画笔与Window“拉伸”,但我不希望中心角度变形。

The deformed background brush

我只需要能够在Grid的背景中绘制形状。如何避免变形?

我写的代码如下:

<Window x:Class="WpfApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="60" Width="300">
    <Window.Resources>
        <DrawingBrush x:Key="GridBackground">
            <DrawingBrush.Drawing>
                <DrawingGroup>
                    <DrawingGroup.Children>
                        <GeometryDrawing Geometry="M0,1 0,0 0.4,0 0.45,0.5 0.4,1Z" Brush="#FF6A00" />
                        <GeometryDrawing Geometry="M0.6,1 0.55,0.5 0.6,0 1,0 1,1Z" Brush="#FF0000" />
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Window.Resources>
    <Grid Background="{StaticResource GridBackground}">
        <TextBlock Foreground="White" VerticalAlignment="Center">Some text</TextBlock>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

我会把它做成两个画笔,一个固定在右边,一个固定在左边。像这样:

<Grid>
     <GeometryXXX Geometry="M0,1 0,0 0.4,0 0.45,0.5 0.4,1Z" Width="300" HorizontalAlignment="Left" Brush="#FF6A00">
     <GeometryXXX Geometry="M0,1 0,0 0.4,0 0.45,0.5 0.4,1Z" Width="300" HorizontalAlignment="Right" Brush="#FF0000">
     <TextBlock Foreground="White" VerticalAlignment="Center">Some text</TextBlock>
</Grid>

我没有打开编译器,也不记得Geometry绘图对象的名称。

另一种方法是创建一个valueconverter,并执行以下操作:

...
    <GeometryDrawing Geometry="{Binding Width, ValueConverter=LeftAngledThing}" Brush="#FF6A00" />
    <GeometryDrawing Geometry="{Binding Width, ValueConverter=LeftAngledThing}" Brush="#FF0000" />
...

你需要查找确切的语法,但是我现在还不记得了。