2009-06-23 114 views
2

我正在XAML中编写画笔,可以使用它来绘制Grid的背景以创建横幅。它看起来像这样:防止在调整大小时在XAML Grid的背景中形变

An example of the brush applied to the background of a Grid http://i40.tinypic.com/8wl012.png

我想刷“拉伸”与GridWindow调整大小,但我不想中心角变形。

The deformed background brush http://i39.tinypic.com/2nly98p.png

我只需要能够绘制形状的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> 

回答

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> 

我没有打开我的编译器,我不记得几何图形对象的名称。

做这将是创建一个valueconverter,以及这样做的另一种方法:

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

你需要查找如何做到这一点,虽然确切的语法,因为我不现在记住它。

+0

形状在网格的背景中很重要。我想对Grid本身做的唯一更改是添加Background属性。 – 2009-06-23 02:18:20