2017-10-14 4 views
0

Here il est écrit:Wpf Geometry.Transform La propriété peut être TransformCollection?

Property Value

Transform

The transformation applied to the Geometry. Note that this value may be a single Transform or a TransformCollection cast as a Transform.

Mais mon code donne une erreur:

myGeometry.Transform = (Transform)new TransformCollection(new Transform[] { 
     new TranslateTransform(33, 22), 
     new ScaleTransform(2, 1) 
    }); 

Cannot convert type 'System.Windows.Media.TransformCollection' to 'System.Windows.Media.Transform'

Quelqu'un at-il une explication? J'ai peut-être tort?

Répondre

1

TransformCollection n'est pas un type dérivé de Transform, il n'y a pas non plus d'opérateur explicite le convertissant au type Transform. Vous devriez le faire comme ceci:

myGeometry.Transform = new TransformGroup 
{ 
    Children = new TransformCollection 
    { 
     new TranslateTransform(33, 22), 
     new ScaleTransform(2, 1) 
    } 
}; 

Ou plus simple, sans créer une nouvelle instance de TransformCollection du tout:

var transform = new TransformGroup(); 
transform.Children.Add(new TranslateTransform(33, 22)); 
transform.Children.Add(new ScaleTransform(2, 1)); 
myGeometry.Transform = transform;