2016-08-26 6 views
4

TextBoxで "Enter"を押すと、アニメーションを開始します。私は右のイベントを発見していないので、私は、次のTextBoxでEnterキーを押したときにアニメーションを開始する方法

XAMLを行っている

<Window.DataContext> 
     <local:MainViewModel/> 
    </Window.DataContext> 
    <Window.Resources> 
     <Storyboard x:Key="testAnimation"> 
      <BooleanAnimationUsingKeyFrames 
            FillBehavior="HoldEnd" 
            Storyboard.TargetName="txtB" 
            Storyboard.TargetProperty="IsEnabled"> 
       <DiscreteBooleanKeyFrame KeyTime="00:00:00" 
                Value="False" /> 
      </BooleanAnimationUsingKeyFrames> 
      <DoubleAnimation 
            Storyboard.TargetName="rtbl" 
            Storyboard.TargetProperty="Opacity" 
            From="0" 
            To="1" 
            Duration="0:0:2" 
            AutoReverse="True"> 
      </DoubleAnimation> 
      <BooleanAnimationUsingKeyFrames 
            FillBehavior="HoldEnd" 
            Storyboard.TargetName="txtB" 
            Storyboard.TargetProperty="IsEnabled" 
            BeginTime="0:0:4" 
            > 
       <DiscreteBooleanKeyFrame KeyTime="00:00:00" 
                Value="True" /> 
      </BooleanAnimationUsingKeyFrames> 
     </Storyboard> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"></RowDefinition> 
      <RowDefinition Height="Auto"></RowDefinition> 
     </Grid.RowDefinitions> 
     <TextBlock Grid.Row="0" Name="rtbl" Opacity="0" HorizontalAlignment="Center" Margin="10" FontSize="30" Text="{Binding Text}"></TextBlock> 
     <TextBox Grid.Row="1" Name="txtB" IsEnabled="True" HorizontalAlignment="Center" 
       Margin="10" FontSize="30" Text="{Binding Input, UpdateSourceTrigger=PropertyChanged}" MinWidth="150" 
       > 
      <TextBox.InputBindings> 
       <KeyBinding Command="{Binding NextCommand}" Key="Enter" 
        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"> 
       </KeyBinding> 
      </TextBox.InputBindings> 
     </TextBox> 
    </Grid> 
</Window> 

MainViewModel

type MainViewModel() as self = 
    inherit ViewModelBase() 

    let rand = Random() 

    let text = self.Factory.Backing(<@ self.Text @>, rand.Next()) 
    let input = self.Factory.Backing(<@ self.Input @>, "") 

    let goNext (param:obj) = 
     text.Value <- rand.Next() 
     input.Value <- "" 
     let control = param :?> Window 
     let animation = control.FindResource("testAnimation") :?> Storyboard 
     animation.Begin() 

    let nextcommand = self.Factory.CommandSyncParam(goNext) 

    member self.NextCommand = nextcommand 
    member self.Text with get() = text.Value 
    member self.Input with get() = input.Value and set v = input.Value <- v 

それは動作しますが、私が見つけるしたいのですがパラメータとして制御を渡すことなく、より良い方法

答えて

4

動作しますが、パラメータとしてコントロールを渡すことなく、より良い方法を見つけたいと思います。

アニメーションを開始すると、これは「純粋なビュー」関連のコードです。

私は実際にあなたのViewModelからあなたのビューに移動します。

コマンドを使用する代わりに、TextBoxのPreviewTextInputイベントを購読して、View自体のコードの後ろにあるアニメーションの開始を処理します。

+0

私は不思議です、F#のカスタムコントロールにRoutedEventを登録することは可能ですか?(ここでは行なわれている行で(https://msdn.microsoft.com/en-us/library/ms752288) .aspx))?そこで、 'を使用してXAMLのイベントを購読することができます。 – Funk

+2

@Funkそれは同じように動作するはずです - 私はこの場合重い手で何かをやっているとは思いません。 –

関連する問題