以下代碼成功更改了單擊Grid時的寬度和高度myTextBox。我希望能夠做的是在單擊 TextBox 時對過渡進行影片處理。
myTextBox單擊/聚焦時如何為以下過渡設定影片?
<Grid HorizontalAlignment="Center" Margin="0,-180,0,0">
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Setter Property="Background" Value="White"/>
<Setter Property="Width" Value="350"/>
<Setter Property="Height" Value="150"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=myTextBox, Path=IsFocused}" Value="True">
<Setter Property="Background" Value="#FFF7F7F7" />
<Setter Property="Width" Value="320" />
<Setter Property="Height" Value="120" />
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
uj5u.com熱心網友回復:
您可以在Storyboards 下放置一個s TextBox.Triggers,它應該在它GotFocus/時運行LostFocus:
<!-- Your grid, width and height of which should be animated -->
<Grid x:Name="myGrid" HorizontalAlignment="Center" >
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Setter Property="Background" Value="White"/>
<Setter Property="Width" Value="350"/>
<Setter Property="Height" Value="150"/>
</Style>
</Grid.Style>
<!-- Your textbox (inside grid), that should trigger animation when Got/Lost focus -->
<TextBox x:Name="myTextBox">
<TextBox.Triggers>
<!-- When TextBox got focus -->
<EventTrigger RoutedEvent="TextBox.GotFocus">
<BeginStoryboard>
<Storyboard>
<!-- Animating width -->
<DoubleAnimation Storyboard.TargetName="myGrid"
Storyboard.TargetProperty="(Grid.Width)"
From="350" To="320" Duration="0:0:0.1">
</DoubleAnimation>
<!-- Animating height -->
<DoubleAnimation Storyboard.TargetName="myGrid"
Storyboard.TargetProperty="(Grid.Height)"
From="150" To="120" Duration="0:0:0.1">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<!-- When TextBox lost focus -->
<EventTrigger RoutedEvent="TextBox.LostFocus">
<BeginStoryboard>
<Storyboard>
<!-- Animating width -->
<DoubleAnimation Storyboard.TargetName="myGrid"
Storyboard.TargetProperty="(Grid.Width)"
From="320" To="350" Duration="0:0:0.1">
</DoubleAnimation>
<!-- Animating height -->
<DoubleAnimation Storyboard.TargetName="myGrid"
Storyboard.TargetProperty="(Grid.Height)"
From="120" To="150" Duration="0:0:0.1">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBox.Triggers>
</TextBox>
</Grid>
TextBox可能不在里面Grid,你做影片,我放在里面只是作為例子。
示例經過測驗,對我來說效果很好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/378603.html
標籤:xaml
