2016-06-14 2 views
-1

私はこのようにしようとしていますが、私は回転を得ていません。私は、次のようなエラーになっています:私はあなたが望むものを明確にあなたを理解している場合cpを使ってwpfで動的にtextblockを回転させる方法は?

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

あなたは観察を教えていません「それはなっていませんでした」。このコードがプロジェクトのどこに存在するかはわかりません。あなたの質問を徹底的に聞いてください。 –

答えて

0

をテキストブロックをアニメーション化することです!

 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

おかげで、それはうまく動作します。 – Basha

0

これを試してみてください。あなたがRenderTransformにアニメーションを使用することができます。

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

あなたのXAMLでは、RotateTransformを追加することができます。

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

それは設計時にうまく動作しますが、私は実行時に動的に必要です – Basha

関連する問題