如何绑定属性动画值?

时间:2014-06-18 14:36:18

标签: c# xaml controltemplate

 <DoubleAnimation Storyboard.TargetName="mainGrid" Storyboard.TargetProperty="Grid.Width" Duration="00:00:00.5" From="40" To="100"/>

x:网格的名称是mainGrid。 如您所见,这是负责动画的代码。我想要From="mainGrid.ActualWidth“和To="mainGrid.ActualWidth + 10"之类的内容但是如何将这些值绑定到actualwidth属性?

1 个答案:

答案 0 :(得分:0)

您可以使用转换器来完成。请考虑以下标记示例:

<Grid Name="mainGrid" Width="40" xmlns:local="clr-namespace:local">
    <Grid.Resources>
        <local:Plus10Converter x:Key="Plus10Converter"/>    
    </Grid.Resources>
    <Button>
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="mainGrid" Storyboard.TargetProperty="Width" Duration="00:00:00.5" 
                                             From="{Binding ElementName=mainGrid, Path=ActualWidth}" 
                                             To="{Binding ElementName=mainGrid, Path=ActualWidth, Converter={StaticResource Plus10Converter}}"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Button.Triggers>
    </Button>
</Grid>

这是背后的代码:

namespace local
{
    public class Plus10Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is double == false) return null;
            return (double)value + 10.0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}