2014-09-12 5 views
0

Visual Studio 용 Blend를 사용하여 가장 간단한 WPF 응용 프로그램을 만들었습니다.WPF에서 동적으로 GoToState를 사용할 수 없습니다. 뭐가 잘못 되었 니?

<VisualStateManager.VisualStateGroups> 
    <VisualStateGroup x:Name="VisualStateGroup"> 
     <VisualState x:Name="VisualState"> 
      <Storyboard> 
       <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="textBox"> 
        <EasingColorKeyFrame KeyTime="0" Value="#FFE40404"/> 
       </ColorAnimationUsingKeyFrames> 
      </Storyboard> 
     </VisualState> 
     <VisualState x:Name="VisualState1"> 
      <Storyboard> 
       <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="textBox"> 
        <EasingColorKeyFrame KeyTime="0" Value="#FF23FF00"/> 
       </ColorAnimationUsingKeyFrames> 
      </Storyboard> 
     </VisualState> 
    </VisualStateGroup> 
</VisualStateManager.VisualStateGroups> 
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="KeyUp"> 
      <ei:GoToStateAction StateName="VisualState1"/> 
     </i:EventTrigger> 
     <i:EventTrigger EventName="KeyDown"> 
      <ei:GoToStateAction StateName="VisualState"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</TextBox> 
: 이것은 내 XAML입니다

public MainWindow() 
{ 
    InitializeComponent(); 
    textBox.MouseEnter+=new System.Windows.Input.MouseEventHandler(textBox_MouseEnter); 
} 
private void textBox_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) 
{ 
    // This will be an empty list 
    var states = VisualStateManager.GetVisualStateGroups(textBox); 
    // This will be false 
    bool thisReturnsFalse = VisualStateManager.GoToState(textBox, "VisualState1", true); 
} 

:

내 문제는 내가 동적 GotoState에이 코드를 사용하지 않을 수 있다는 것입니다 :

나는 하나의 텍스트 상자에 대한 간단한 시각적 상태 그룹을 생성

답변

1

VisualStateGroupsTextBox에 정의되어 있지 않으므로

var states = VisualStateManager.GetVisualStateGroups(textBox); 

은 예상 한 것을 반환하지 않습니다. 당신이 정의 된 상태를 얻고 싶은 경우에, (나는 여기 Window 있으리라 믿고있어) 즉 VisualStateGroups 들어있는 FrameworkElement 통과 : 같은 아마 VisualStateManager.GoToState에 호출에 사용되어야한다

var states = VisualStateManager.GetVisualStateGroups(this); 

:

bool shouldReturnTrue = VisualStateManager.GoToState(this, "VisualState1", true); 
+0

내 GoToState 호출은 이벤트에서 온 것입니다. XAML의 TextBox에서 VisualStateGroups를 이동해야했지만 다음과 같이 나타납니다. /* 예상대로 1입니다. */ var stateGroups = VisualStateManager.GetVisualStateGroups (textBox); /* 여전히 false입니다. */ bool thisReturnsFalse = VisualStateManager.GoToState (this, "VisualState1", false);/* 또한 내 KeyUp, KeyDown이 작동하지 않습니다. */ – profimedica

+1

@profimedica는 질문에 XAML이 표시하지 않으므로 이러한 변경 사항을 반영하도록 XAML을 업데이트합니다. –

+2

@profimedica GoToState 대신 GoToElementState 호출 –