Silverlight ProgressBar:动态设置IndeterminateGradientFill?

时间:2011-04-25 17:24:26

标签: animation silverlight-4.0 binding progress-bar

我在SL4中设计了一个ProgressBar,并且除了不确定的动画外几乎完成了它。这是一种打字样式,ProgressBar的宽度不是固定值。我试图在故事板中设置“To”值,但我没有取得多大成功......

<DoubleAnimation Duration="00:00:.5" Storyboard.TargetProperty="(Shape.Fill).(LinearGradientBrush.Transform).(TransformGroup.Children)[0].X" Storyboard.TargetName="IndeterminateGradientFill" From="0" To="{Binding ElementName=ProgressBarRootGrid, Path=ActualWidth}"/>

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

我建议将Shape的Horizo​​ntalAlignment设置为Stretch,然后为RenderTransforms Scale X属性设置动画。

这应该是你的开始:

<UserControl x:Class="SilverlightTestImages.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <ControlTemplate x:Key="ProgressBarControlTemplate1" TargetType="ProgressBar">
            <Grid>
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Determinate"/>
                        <VisualState x:Name="Indeterminate">
                            <Storyboard RepeatBehavior="Forever">
                                <DoubleAnimation Duration="0:0:1" To="1" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Storyboard.TargetName="rectangle" d:IsOptimized="True"/>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <Border BorderBrush="Black" BorderThickness="1" CornerRadius="2">
                    <Rectangle x:Name="rectangle" Fill="#FF1B6C0B" RadiusX="1" RadiusY="1" RenderTransformOrigin="0,0.5">
                        <Rectangle.RenderTransform>
                            <CompositeTransform ScaleX="0"/>
                        </Rectangle.RenderTransform>
                    </Rectangle>
                </Border>
            </Grid>
        </ControlTemplate>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <ProgressBar Height="10" VerticalAlignment="Bottom" Template="{StaticResource ProgressBarControlTemplate1}" IsIndeterminate="True"/>
    </Grid>
</UserControl>

更新

为了完成与VS2010类似的操作,由于某些方式设置了翻译动画,因此进度条有点难度。来自Silverlight测试团队的Jeff Wilcox有一个很好的控件,可以满足您的需求。转到http://www.jeff.wilcox.name/2010/08/performanceprogressbar/,在“获取代码”下,下载RelativeAnimatingContentControl.cs。然后使用他跟随动画,这应该接近你正在寻找的

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:Microsoft_Phone_Controls_Unsupported="clr-namespace:Microsoft.Phone.Controls.Unsupported" 
    x:Class="SilverlightTestImages.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <ControlTemplate x:Key="ProgressBarControlTemplate1" TargetType="ProgressBar">
            <Microsoft_Phone_Controls_Unsupported:RelativeAnimatingContentControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" >
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Determinate"/>
                        <VisualState x:Name="Indeterminate">
                            <Storyboard RepeatBehavior="Forever">
                                <DoubleAnimation Duration="0:0:1" To="100.1" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="Background" d:IsOptimized="True"/>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <Border BorderBrush="#FFBCBCBC" BorderThickness="1" CornerRadius="2">
                    <Border.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFF5F5F5"/>
                            <GradientStop Color="#FFE5E5E5" Offset="0.49"/>
                            <GradientStop Color="#FFB4B4B4" Offset="0.5"/>
                            <GradientStop Color="#FFC8C8C8" Offset="1"/>
                        </LinearGradientBrush>
                    </Border.Background>
                    <Grid>
                        <Border BorderBrush="#B2FFFFFF" BorderThickness="1" CornerRadius="2"/>
                        <Rectangle x:Name="Background" RadiusX="1" RadiusY="1" RenderTransformOrigin="0,0.5" Width="100" HorizontalAlignment="Left">
                            <Rectangle.Fill>
                                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                                    <GradientStop Color="#0041DE0C"/>
                                    <GradientStop Color="#9941DE0C" Offset="0.4"/>
                                    <GradientStop Color="#9941DE0C" Offset="0.6"/>
                                    <GradientStop Color="#0041DE0C" Offset="1"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                            <Rectangle.RenderTransform>
                                <CompositeTransform TranslateX="-20.1"/>
                            </Rectangle.RenderTransform>
                        </Rectangle>
                    </Grid>
                </Border>
            </Microsoft_Phone_Controls_Unsupported:RelativeAnimatingContentControl>
        </ControlTemplate>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <ProgressBar Height="20" Width="200" VerticalAlignment="Center" HorizontalAlignment="Center" Template="{StaticResource ProgressBarControlTemplate1}" IsIndeterminate="True" />
    </Grid>
</UserControl>