在我看來,你需要爲了從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();
}
}
請考慮,它是不可能在運行時改變動畫的持續時間。您可以在空閒時更改它。
你是否得到這個綁定的輸出錯誤? – adminSoftDK
我沒有得到任何錯誤,但我從來沒有讓XAML給我一個運行時錯誤,所以我不知道它是否被無聲地破壞了 –