我在故事板中使用以下代码旋转3D房屋:
<DoubleAnimation Storyboard.TargetName="HouseRotate" Storyboard.TargetProperty="Angle" From="0" To="-359" Duration="{Binding StringFormat=0:0:{0}, ElementName=slDuration, Path=Value, UpdateSourceTrigger=PropertyChanged}" RepeatBehavior="Forever"/>
我希望持续时间属性根据滑块值更改,但它似乎停留在1秒。我知道滑块的值正在改变,因为我已将其输出到标签。
答案 0 :(得分:1)
在我看来,您需要一个合适的转换器才能将double
(Slider
的属性值返回double
)转换为Duration结构。
看看这个简单的例子:
<Window.Resources>
<local:DoubleToDurationConverter x:Key="DoubleToDurationConverter" />
</Window.Resources>
<StackPanel>
<Button Content="Click me for an animation" Margin="20" Padding="20">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="Green"
Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)"
FillBehavior="Stop"
Duration="{Binding ElementName=slider, Path=Value, Mode=OneWay, Converter={StaticResource DoubleToDurationConverter}}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<Slider Name="slider" Minimum="10" Maximum="40" Value="30"
Margin="5" AutoToolTipPlacement="TopLeft" IsSnapToTickEnabled="True" />
</StackPanel>
转换代码为:
public class DoubleToDurationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Duration duration = new Duration(TimeSpan.FromSeconds((double)value));
return duration;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
请注意,在动画运行时无法更改动画的持续时间。您可以在空闲时更改它。