2012-09-07 46 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