Search code examples
wpfxamlpathgeometry

Why can't I define a Geometry with a Transform in XAML?


I am trying to create a ResourceDictionary á la this answer that contains StreamGeometries that have Transforms set:

<ResourceDictionary>
    <StreamGeometry x:Name="Chevrons">
        <StreamGeometry.Transform>
            <TranslateTransform X="20" Y="120"/>
        </StreamGeometry.Transform>
        M21.750001,94.749999 L34.000002,117.66218 30.625003,133.62501 17 [...]
    </StreamGeometry>
</ResourceDictionary>

However, I get the following error:

1: Cannot add content to an object of type "StreamGeometry".

and

2: TypeConverter syntax error encountered while processing initialization string '{PathData}'. Element attributes are not allowed on objects created via TypeConverter.

So I tried it with a PathGeometry and got this error:

The specified value cannot be assigned to the collection. The following type was expected: "PathFigure".

Is there any way to do apply a transform to a Geometry in XAML code? Or do I just have to do it via code?


Solution

  • You may write it like this:

    <PathGeometry x:Key="Chevrons">
        <PathGeometry.Transform>
            <TranslateTransform X="20" Y="120"/>
        </PathGeometry.Transform>
        <PathGeometry.Figures>
            M21.750001,94.749999 L34.000002,117.66218 30.625003,133.62501 ...
        </PathGeometry.Figures>
    </PathGeometry>
    

    or like this:

    <PathGeometry x:Key="Chevrons"
        Figures="M21.750001,94.749999 L34.000002,117.66218 30.625003,133.62501 ...">
        <PathGeometry.Transform>
            <TranslateTransform X="20" Y="120"/>
        </PathGeometry.Transform>
    </PathGeometry>