2015-09-20 65 views

回答

1

如果要生成graphicaly,请在Blend中打开项目。

打开触发选项卡(第三个选项卡)
+事件
在当部分中,选择出了名的文本框,并在事件LostFocus(或的GotFocus)
点击的+右“是提出了”以创建故事板
在对象和时间线窗口中,选择文本框,并选择在200毫秒的点(2线右侧)
然后在属性窗口,变换部,应用旋转:
对于文本框在属性窗口,LayoutTransform部分,编辑:
-r的中心浮选在第五个标签(0,0),在第二个选项卡(-90)

这里旋转
-Angle是生成的代码:

<Window x:Class="DemoRotateWithTrigger.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <Storyboard x:Key="OnLostFocus1"> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="textboxToBeRotated"> 
      <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="-90"/> 
     </DoubleAnimationUsingKeyFrames> 
     <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" Storyboard.TargetName="textboxToBeRotated"> 
      <EasingPointKeyFrame KeyTime="0:0:0.2" Value="0,0"/> 
     </PointAnimationUsingKeyFrames> 
    </Storyboard> 
    <Storyboard x:Key="OnGotFocus1"> 
     <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" Storyboard.TargetName="textboxToBeRotated"> 
      <EasingPointKeyFrame KeyTime="0:0:0.2" Value="0,0"/> 
     </PointAnimationUsingKeyFrames> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="textboxToBeRotated"> 
      <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/> 
     </DoubleAnimationUsingKeyFrames> 
    </Storyboard> 
</Window.Resources> 
<Window.Triggers> 
    <EventTrigger RoutedEvent="UIElement.LostFocus" SourceName="textboxToBeRotated"> 
     <BeginStoryboard Storyboard="{StaticResource OnLostFocus1}"/> 
    </EventTrigger> 
    <EventTrigger RoutedEvent="UIElement.GotFocus" SourceName="textboxToBeRotated"> 
     <BeginStoryboard x:Name="OnGotFocus1_BeginStoryboard" Storyboard="{StaticResource OnGotFocus1}"/> 
    </EventTrigger> 
</Window.Triggers> 
<Grid> 
    <TextBox x:Name="textboxToBeRotated" HorizontalAlignment="Left" Height="23" Margin="138,158,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0,0"> 
     <TextBox.RenderTransform> 
      <TransformGroup> 
       <ScaleTransform/> 
       <SkewTransform/> 
       <RotateTransform Angle="-90"/> 
       <TranslateTransform/> 
      </TransformGroup> 
     </TextBox.RenderTransform> 
    </TextBox> 
    <Button Content="Button" HorizontalAlignment="Left" Margin="75,219,0,0" VerticalAlignment="Top" Width="75"/> 
</Grid> 
</Window> 

好运

+0

谢谢,干得不错。 –

1

RenderTransformOrigin = new Point(0, 0)并设置初始RenderTransform = new RotateTransform(-90)Loaded窗口的事件,然后将其设置为GotFocus0LostFocus再次-90

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    this.TextBox1.RenderTransformOrigin = new Point(0, 0); 
    this.TextBox1.RenderTransform = new RotateTransform(-90); 
} 

private void TextBox1_GotFocus(object sender, RoutedEventArgs e) 
{ 
    this.TextBox1.RenderTransform = new RotateTransform(0); 
} 


private void TextBox1_LostFocus(object sender, RoutedEventArgs e) 
{ 
    this.TextBox1.RenderTransform = new RotateTransform(-90); 
} 

记得在窗口中有另一个控件来测试LostFocus

+0

谢谢它的工作原理,但我要求xaml触发器。 –

+0

问题是“我怎样才能在xaml中翻转TextBox?”。这个答案是C#。 –