wpf C#中如何旋转textblock中的文字(Code-Behind)~~
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9060409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you rotate the text in textblock in C# (Code-Behind)~~
提问by Joon Kiat
Basically I am currently doing final year project in my college whereby i am touching on surface 2.0 WPF.
基本上我目前正在我的大学做最后一年的项目,我正在接触表面 2.0 WPF。
My project is a game whereby if a user answer a question wrongly,the next question will be rotated to make it more difficult. But I am unsure how to do it. I saw an example in msdn microsoft but it only shows XAML codes. I need the C# codes.
我的项目是一个游戏,如果用户回答错误,下一个问题将被轮换以增加难度。但我不确定该怎么做。我在 msdn microsoft 中看到了一个示例,但它只显示 XAML 代码。我需要 C# 代码。
Here's the XAML example.
这是 XAML 示例。
http://msdn.microsoft.com/en-us/library/ms754028.aspx
http://msdn.microsoft.com/en-us/library/ms754028.aspx
The last example
最后一个例子
Here's part of my validation codes. I need to activate the animation if user answer wrongly.
这是我的验证代码的一部分。如果用户回答错误,我需要激活动画。
if (surfaceRadioButton1.IsChecked == true)
{
user_answer = (string)surfaceRadioButton1.Content;
textBlock2.Text = validateAnswer(user_answer, answer);
retreiveYellowQns();
if (textBlock2.Text.Equals("Correct"))
{
yellow_coord = yellow_coord + 50;
Canvas.SetLeft(car, yellow_coord);
Canvas.SetTop(car, 289);
}
else
{
if (yellow_coord <= 330)
{
yellow_coord = 330;
Canvas.SetLeft(car, yellow_coord);
Canvas.SetTop(car, 289);
}
else
{
yellow_coord = yellow_coord - 50;
Canvas.SetLeft(car, yellow_coord);
Canvas.SetTop(car, 289);
}
}
}
Any Help will be glad,thanks in advance.
任何帮助都会很高兴,提前致谢。
回答by ElGaucho
Try this one. You can use an animation on the RenderTransform:
试试这个。您可以在 RenderTransform 上使用动画:
var rotateAnimation = new DoubleAnimation(0, 180, TimeSpan.FromSeconds(5));
var rt = (RotateTransform) textblock2.RenderTransform;
rt.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation);
In your Xaml, you can add the RotateTransform:
在您的 Xaml 中,您可以添加 RotateTransform:
<TextBlock>
<TextBlock.RenderTransform>
<RotateTransform Angle="0"/>
</TextBlock.RenderTransform>
</TextBlock>
回答by Maheep
You will have to use Transformation for this. Try this answer https://stackoverflow.com/a/8815374/293712
为此,您必须使用转换。试试这个答案https://stackoverflow.com/a/8815374/293712
Or You can also try, (I have not tried this) Look at this articlefor more details
或者你也可以试试,(这个我没试过)看这篇文章了解更多
textBlock2.RenderTransform = new RotateTransform(IntegerAngleValue);
回答by Andreas
var rotateAnimation = new DoubleAnimation(180, 0, TimeSpan.FromMilliseconds(200));
UiImage.RenderTransformOrigin = new Point(0.5,0.5);
UiImage.RenderTransform = new RotateTransform();
var rt = (RotateTransform)UiImage.RenderTransform;
rt.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation);