Domanda

I have some rectangles which their heights are same. But i filled them with different color. Can i combine them as result i get Rectangle? I can do it with RectangleGeometry but i need Rectangle type

È stato utile?

Soluzione

How do you want to combine the colours?

Do you just want to specify 2 rectangular regions which overlap but use different colors with a level of transparency so that the colours blend together?

Or do you want the Rectangle subdivided and using different colours in different regions?

Is there a reason you need to keep it as a Rectangle?

Here's a way to keep it as a Rectangle but specify your 2 colours to combine/mix as the Fill:

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>

Or this one for subdivided rectangles:

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 described by the DrawingBrush into Resources if you intend to use it in multiple places....and/or create a new Style for Rectangles).

Rectangle is sealed so it can be overridden, and it's not a Control so you can't change a template.

You might want to consider doing your own "Shape", so that you can better encapsulate the enhanced behaviour of "your" Rectangle.

Here's an example to get your started.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top