2016-06-14 15 views
-1

Ich versuche so, aber ich bekomme nicht die Rotation. Ich erhalte folgende Fehlermeldung:Wie man Textblock dynamisch in WPF mit C# drehen?

Unable to cast object of type 'System.Windows.Media.TransformGroup' to type 'System.Windows.Media.RotateTransform'.

TextBlock txt = new TextBlock(); 
txtb.Text="Sample"; 

var rotateAnimation = new DoubleAnimation(0, 270, TimeSpan.FromSeconds(5)); 

var rt = (RotateTransform)txt.RenderTransform; 
rt.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation); 
+3

„Es war nicht immer“ uns nicht sagen, was Sie beobachtet. Wir wissen nicht einmal, wo dieser Code in Ihrem Projekt existiert. Bitte nehmen Sie sich etwas mehr Zeit, um Ihre Frage gründlich zu stellen. –

Antwort

0

wenn ich verstehe Sie klar, was Sie wollen, ist Textblock zu animieren!

 TextBlock txt = new TextBlock();    
     txt.Text = "Sample"; 
     txt.HorizontalAlignment = HorizontalAlignment.Center; 
     txt.VerticalAlignment = VerticalAlignment.Center; 
     RotateTransform r1 = new RotateTransform(); 
     txt.RenderTransform = r1; 
     MainGrid.Children.Add(txt); //MainGrid is the name of your main layout 

     var rotateAnimation = new DoubleAnimation(0, 270, TimeSpan.FromSeconds(5)); 
     rotateAnimation.RepeatBehavior = RepeatBehavior.Forever; 
     var rt = (RotateTransform)txt.RenderTransform; 
     rt.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation); 
+0

Vielen Dank, es funktioniert gut. – Basha

0

Versuchen Sie diese. Sie können eine Animation auf dem Render verwenden:

var rotateAnimation = new DoubleAnimation(0, 270, TimeSpan.FromSeconds(5)); 
var rt = (RotateTransform) textblock2.RenderTransform; 
rt.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation); 

In XAML können Sie die RotateTransform hinzufügen:

<TextBlock> 
    <TextBlock.RenderTransform> 
    <RotateTransform Angle="0"/> 
    </TextBlock.RenderTransform> 
</TextBlock> 
+0

wird es zur Entwurfszeit gut funktionieren, aber ich brauche dynamisch zur Laufzeit – Basha

Verwandte Themen