简体   繁体   中英

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?

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>

About the behavior of StreamGeometry , quote from MSDN :

Here:

A StreamGeometry is a Freezable type. StreamGeometry is light-weight alternative to PathGeometry for creating geometric shapes. Use a StreamGeometry when you need to describe a complex geometry but do not want the overhead of supporting data binding, animation, or modification. Because of its efficiency, the StreamGeometry class is a good choice for describing adorners.

And here:

A StreamGeometry cannot be serialized if it contains a Transform or any non-stroked or unfilled segments.

Therefore, use the PathGeomerty , as advised @Clemens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM