none
[WPF] Converter doesn't update Duration first time. RRS feed

  • Question

  • Converter doesn't update Duration of DoubleAnimation first time.

    Animation finishes quickly, when it should in one minute. Its Duration stays Automatic.

    I suppose it can't catch in time, because if I begin Storyboard some time after I loaded it, it works.

    How to fix it?

    MainWindow.xaml:

        <Window.Resources>
            <local:DurationConverter x:Key="DurationConverter"></local:DurationConverter>
            <Storyboard x:Key="OnMouseDown1">
                <DoubleAnimation Storyboard.TargetProperty="Width" Storyboard.TargetName="rectangle" To="500"
                                 Duration="{Binding Converter={StaticResource DurationConverter}}">
                </DoubleAnimation>
            </Storyboard>
        </Window.Resources>
        <Grid Background="Black">
            <Rectangle x:Name="rectangle" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Fill="White"/>
            <Rectangle x:Name="rectangle1" Fill="Red" HorizontalAlignment="Left" Width="100" Height="100" VerticalAlignment="Bottom" MouseDown="rectangle1_MouseDown"/>
        </Grid>

    MainWindow.xaml.cs:

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void rectangle1_MouseDown(object sender, MouseButtonEventArgs e)
            {
                Storyboard up = App.Current.MainWindow.Resources["OnMouseDown1"] as Storyboard;
                up.Begin();
            }
        }
    DurationConverter.cs
        class DurationConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return new System.Windows.Duration(TimeSpan.FromMinutes(1));
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    Tuesday, July 26, 2016 9:27 AM

Answers