wpfdrawingbrushgeometrydrawing

WPF. GeometryDrawing's Brush is always only Transparent


Here is a test app that just shows a hatched ellipse in a window. No matter how I write it, the background of the ellipse is transparent; not the color I'm setting the GeometryDrawing. I'm stumped. The resulting picture shows the background of the ellipse as transparent when it should be Green.

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:WpfApp2"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Background="Red"
        mc:Ignorable="d">
   <Window.Resources>
      <ResourceDictionary>
         <DrawingBrush x:Key="HatchBrush"
                       Stretch="UniformToFill"
                       TileMode="Tile"
                       Viewbox="0 0 10 10"
                       ViewboxUnits="Absolute"
                       Viewport="0 0 10 10"
                       ViewportUnits="Absolute">
            <DrawingBrush.Drawing>
               <GeometryDrawing Brush="Green">
                  <GeometryDrawing.Geometry>
                     <GeometryGroup>
                        <LineGeometry StartPoint="0 0"
                                      EndPoint="10 0" />
                        <LineGeometry StartPoint="0 0"
                                      EndPoint="0 10" />
                     </GeometryGroup>
                  </GeometryDrawing.Geometry>
                  <GeometryDrawing.Pen>
                     <Pen Brush="Yellow"
                          EndLineCap="Square"
                          StartLineCap="Square"
                          Thickness="3" />
                  </GeometryDrawing.Pen>
               </GeometryDrawing>
            </DrawingBrush.Drawing>
         </DrawingBrush>
       </ResourceDictionary>
   </Window.Resources>
   <Grid Margin="5"
         HorizontalAlignment="Stretch"
         VerticalAlignment="Stretch">
      <Ellipse x:Name="c_Ellipse"
               HorizontalAlignment="Stretch"
               VerticalAlignment="Stretch"
               Fill="{StaticResource HatchBrush}"
               Stroke="Black" />
   </Grid>
</Window>

This is what I get: Ellipse with a transparent background rather than the assigned Green

The background of the ellipse should be Green:

<GeometryDrawing Brush="Green">

Solution

  • There is no filled geometry that uses the GeometryDrawing's Brush.

    You may use a RectangleGeometry instead of two LineGeometries:

    <GeometryDrawing Brush="Green">
        <GeometryDrawing.Geometry>
            <RectangleGeometry Rect="0,0,10,10"/>
        </GeometryDrawing.Geometry>
        <GeometryDrawing.Pen>
            <Pen Brush="Yellow" Thickness="1.5"/>
        </GeometryDrawing.Pen>
    </GeometryDrawing>