2009-06-23 113 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