2012-09-07 67 views
1

我有一些高度相同的矩形。但我填補了他們不同的顏色。我可以結合他們,因爲我得到Rectangle?我可以用RectangleGeometry做到這一點,但我需要Rectangle類型有沒有機會將矩形合併到一個矩形?

+0

你想[Rectangle類](http://msdn.microsoft.com/en-us/library/system.windows.shapes.rectangle)或[矩形結構]( http://msdn.microsoft.com/en-us/library/system.drawing.rectangle.aspx)? – LPL

+0

長方形課程當然 – Rabi

回答

3

如何組合顏色?

你只是想指定重疊,但使用不同的顏色與透明度級別,這樣的顏色混合在一起2個矩形區域?

還是你想的矩形細分,並在不同的區域用不同的顏色?

是否有你需要保持它作爲一個矩形的理由?

下面就來保持它作爲一個矩形,但指定2種顏色組合/混合作爲填充方式:

enter image description here

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid> 
    <Rectangle Width="100" Height="100"> 
    <Rectangle.Fill> 
    <DrawingBrush Viewport="0,0,1,1" TileMode="Tile"> 
     <DrawingBrush.Drawing> 
      <DrawingGroup> 
       <GeometryDrawing> 
        <GeometryDrawing.Geometry> 
         <RectangleGeometry Rect="0,0,1,1" /> 
        </GeometryDrawing.Geometry> 
        <GeometryDrawing.Brush> 
         <SolidColorBrush Color="Red" Opacity="1"/> 
        </GeometryDrawing.Brush> 
       </GeometryDrawing> 
       <GeometryDrawing> 
        <GeometryDrawing.Geometry> 
         <RectangleGeometry Rect="0,0,1,1" /> 
        </GeometryDrawing.Geometry> 
        <GeometryDrawing.Brush> 
         <SolidColorBrush Color="White" Opacity=".5"/> 
        </GeometryDrawing.Brush> 
       </GeometryDrawing> 
      </DrawingGroup> 
     </DrawingBrush.Drawing> 
    </DrawingBrush> 
    </Rectangle.Fill> 
    </Rectangle> 
    </Grid> 
</Page> 

或者這一個細分矩形:

enter image description here

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid> 
    <Rectangle Width="100" Height="100"> 
    <Rectangle.Fill> 
    <DrawingBrush Viewport="0,0,1,1" TileMode="None"> 
     <DrawingBrush.Drawing> 
      <DrawingGroup> 
       <GeometryDrawing> 
        <GeometryDrawing.Geometry> 
         <RectangleGeometry Rect="0,0,1,1" /> 
        </GeometryDrawing.Geometry> 
        <GeometryDrawing.Brush> 
         <SolidColorBrush Color="Yellow"/> 
        </GeometryDrawing.Brush> 
       </GeometryDrawing> 
       <GeometryDrawing> 
        <GeometryDrawing.Geometry> 
         <RectangleGeometry Rect="0,0,0.5,0.5" /> 
        </GeometryDrawing.Geometry> 
        <GeometryDrawing.Brush> 
         <SolidColorBrush Color="Red"/> 
        </GeometryDrawing.Brush> 
       </GeometryDrawing> 
       <GeometryDrawing> 
        <GeometryDrawing.Geometry> 
         <RectangleGeometry Rect="0.5,0.5,0.5,0.5" /> 
        </GeometryDrawing.Geometry> 
        <GeometryDrawing.Brush> 
         <SolidColorBrush Color="Green"/> 
        </GeometryDrawing.Brush> 
       </GeometryDrawing> 
       <GeometryDrawing> 
        <GeometryDrawing.Geometry> 
         <RectangleGeometry Rect="0.25,0.25,0.25,0.25" /> 
        </GeometryDrawing.Geometry> 
        <GeometryDrawing.Brush> 
         <SolidColorBrush Color="Blue"/> 
        </GeometryDrawing.Brush> 
       </GeometryDrawing> 
      </DrawingGroup> 
     </DrawingBrush.Drawing> 
    </DrawingBrush> 
    </Rectangle.Fill> 
    </Rectangle> 
    </Grid> 
</Page> 

(move the brush descr如果你打算在多個地方使用它,並且/或者爲矩形創建一個新的樣式,則由DrawingBrush加入到Resources中。

矩形是密封的,因此它可以被覆蓋,所以你不能更改模板它不是一個控制。

你可能要考慮做自己的「形狀」,這樣就可以更好地封裝「你的」矩形的增強行爲。

下面就來讓你開始了一個例子。

+0

讓我以另一種方式說。我有'List ',我想把這個'List'合併成一個'Rectangle'。我可以使用你的解決方案。我在後面的代碼中創建了'Rectangles' – Rabi