2010-04-08 5 views
10

좋아, 나는 정말로 나를 잡아 끌고있는 뭔가를 만났다.WPF에서 TextBox.Text를 애니메이션화 할 수없는 이유는 무엇입니까?

몇 가지 관련없는 질문으로 동료 개발자를 돕고 있었고 그의 프로젝트에서 TextBlock (s)으로 텍스트 애니메이션을 적용하고있었습니다. 그래서, 나는 다시 그의 책상에 가서 그의 질문에 답하기 위해 프로젝트를 다시 만들었지 만, 실수로 TextBlock 대신에 TextBox을 사용했습니다. 내 텍스트가 전혀 애니메이션이 아니 었습니다! (많은 도움, 나는 있었다!)

결국, 그의 xaml이 TextBlock을 사용하고 있고 TextBox를 사용하고 있다는 것을 알았습니다. 흥미로운 점은 TextBox를 사용할 때 블렌드가 키 프레임을 만들지 못했기 때문입니다. 그래서 TextBlock을 사용하여 Blend에서 작업 한 다음 xaml을 직접 수정하여 TextBlock을 TextBox로 변환했습니다. 혼합 애니메이션에서 키 프레임을 생성 알고 ...가 아니라 똑똑 것처럼 그냥 것 (

InvalidOperationException: '(0)' Storyboard.TargetProperty path contains nonanimatable property 'Text'.

글쎄, 그것은 보인다 내가 프로젝트를 실행하면, 다음과 같은 오류가 발생했습니다 TextBox에서 직접 값을 수정하십시오.) 블렌드 +1.

그래서 질문이 생겼습니다. 왜 TextBox.Text를 애니메이션으로 만들 수 없습니까? 보통 대답은 애니메이트하는 특정 속성이 DependencyProperty가 아니라는 것입니다. 그러나이 경우, TextBox.Text DependencyProperty입니다.

그래서 지금 당황하고 있습니다! 왜 TextBox.Text에 애니메이션을 적용 할 수 없습니까?


문제를 설명하기 위해 일부 xaml을 포함 시키십시오. 다음 xaml은 작동하지만 TextBlock (s)을 사용합니다.

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="TextBoxTextQuestion.MainWindow" 
    x:Name="Window" 
    Title="MainWindow" 
    Width="640" 
    Height="480" 
> 
    <Window.Resources> 
     <Storyboard x:Key="animateTextStoryboard"> 
      <StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="textControl"> 
       <DiscreteStringKeyFrame KeyTime="0:0:1" Value="Goodbye"/> 
      </StringAnimationUsingKeyFrames> 
     </Storyboard> 
    </Window.Resources> 
    <Window.Triggers> 
     <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
      <BeginStoryboard Storyboard="{StaticResource animateTextStoryboard}"/> 
     </EventTrigger> 
    </Window.Triggers> 
    <Grid x:Name="LayoutRoot"> 
     <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center"> 
      <TextBlock x:Name="textControl" Text="Hello" FontFamily="Calibri" FontSize="32"/> 
      <TextBlock Text="World!" Margin="0,25,0,0" FontFamily="Calibri" FontSize="32"/> 
     </StackPanel> 
    </Grid> 
</Window> 

XAML 작동 및 사용하지 않는 다음과 같은 TextBox.Text :

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="TextBoxTextQuestion.MainWindow" 
    x:Name="Window" 
    Title="MainWindow" 
    Width="640" 
    Height="480" 
> 
    <Window.Resources> 
     <Storyboard x:Key="animateTextStoryboard"> 
      <StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBox.Text)" Storyboard.TargetName="textControl"> 
       <DiscreteStringKeyFrame KeyTime="0:0:1" Value="Goodbye"/> 
      </StringAnimationUsingKeyFrames> 
     </Storyboard> 
    </Window.Resources> 
    <Window.Triggers> 
     <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
      <BeginStoryboard Storyboard="{StaticResource animateTextStoryboard}"/> 
     </EventTrigger> 
    </Window.Triggers> 
    <Grid x:Name="LayoutRoot"> 
     <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center"> 
      <TextBox x:Name="textControl" Text="Hello" FontFamily="Calibri" FontSize="32"/> 
      <TextBox Text="World!" Margin="0,25,0,0" FontFamily="Calibri" FontSize="32"/> 
     </StackPanel> 
    </Grid> 
</Window> 
+0

흥미 롭다 ... 결코 이것에 대한 필요성을 갖지는 않았지만, 항상 그렇다고 추정했다. 바라건대 누군가가 대답을하기를 바랍니다. 미안해. 도울 수 없어. –

+0

감사합니다. 나는 학문적 인 질문이 조금있다 ...하지만 나는 애니메이션 할 수없는 DependencyProperty를 보는 것이 매우 궁금하다. – cplotts

답변

28

수동으로 텍스트 상자를 애니메이션 시도는 ....

var timeline = new StringAnimationUsingKeyFrames(); 
timeline.KeyFrames.Add(new DiscreteStringKeyFrame("Goodbye", KeyTime.FromTimeSpan(new TimeSpan(0,0,1)))); 
textControl.BeginAnimation(TextBox.TextProperty, timeline); 

...를 보여 더 유용한 오류 메시지.

'Text' property is not animatable on 'System.Windows.Controls.TextBox' class because the IsAnimationProhibited flag has been set on the UIPropertyMetadata used to associate the property with the class.
Parameter name: dp

UIPropertyMetadata.IsAnimationProhibited의 설명서를 말한다 :

In general, the default dependency properties available in the Windows Presentation Foundation (WPF) framework implementation APIs can be animated. You might set this property to true in the metadata of your own custom dependency property to disable animations on it.

는 분명히, WPF 라이브러리의 디자이너가 텍스트 상자의 Text depdendency 속성을 애니메이션하는 것은 아니라고 결정 마지막 줄은 다음 ArgumentException 실패 좋은 아이디어와 명시 적으로 그것을 해제합니다.

그래서이 속성을 애니메이션으로 적용 할 수없는 이유는 기술입니다. 왜 그들은 그것을 무능하게 했습니까? 나는 아무 생각이 ...

PS : 리플렉터와 TextBox, TextBoxBaseControl의 정적 생성자에서 빠른 모습 Text 애니메이션되지 않을 수있는 유일한 텍스트 상자 종속성 속성임을 알 수있다.

+1

좋은 답변입니다! 이것은 나를 미치게했다. 흥미롭게도, TextBox에 정적 생성자 (Reflector 사용)로 설정된 속성 메타 데이터가 표시되지 않습니다. 사실, 질문을 게시하기 전에 그런 속성을 확인했습니다. 그 속성 메타 데이터가 Text 속성으로 설정된 곳이 궁금합니다. – cplotts

+0

'TextBox'의 정적 생성자는'FrameworkPropertyMetadata' 생성자의 오버로드를 사용합니다 : http://msdn.microsoft.com/en-us/library/ms557303.aspx 그리고'isAnimationProhibited' 인수에 대해'true'를 전달합니다. – Heinzi

+0

아, 나는 그것을 놓쳤다. 다시 한 번 좋은 직장. – cplotts

관련 문제