WPF动画边距绑定

时间:2018-05-24 14:04:19

标签: wpf animation data-binding

有没有办法将Thickness动画绑定到UserControls父容器的宽度,方式是动画使UserControl从左侧滑入?

我知道Width是一个int,而target属性是一个不同类型的边距,我知道你应该使用某种ValueConverter但我的似乎没有正常工作。

下面是我非常贫穷,但最好的尝试是正确的。哪个不起作用。还有更简单的方法吗?优选不需要转换器和纯xaml溶液。

<Window>
    <Grid> <!-- Main Window Container -->
        <UserControl x:Name="ParentControl" Content="{Binding Window}" />
    </Grid>
</Window>

<UserControl>
    <UserControl.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <ThicknessAnimation Storyboard.TargetName="ucDashboardView" 
                                    Storyboard.TargetProperty="Margin" 
                                    From="{Binding Width, ElementName=ParentControl, 
                                    Converter=cnv:MarginWidthConverter}"  
                                    To="0" Duration="0:0:1"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </UserControl.Triggers>

    <!-- Just so we can see something right now -->
    <Grid Margin="50" Background="Gainsboro"/>
</UserControl>

然后是C#转换器

public class MarginWidthConverter : BaseValueConverter<MarginWidthConverter>
{
    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return new Thickness(-(int)value, 0, (int)value, 0);
    }

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

0 个答案:

没有答案