Silverlight 3 ProgressBar模板

时间:2009-11-05 11:33:09

标签: progress-bar converter controltemplate templatebinding

我正在尝试重新模拟Silverlight3进度条,在 ProgressBarIndicator 中的TextBlock中显示“Value”,并在 ProgressBarTrack

我认为在 Value Maximum 属性上使用TemplateBinding会执行此操作,但它无法正常工作。

仅供参考我到目前为止的模板是:

        <Style x:Key="ProgressBarStyle1" TargetType="ProgressBar">
        <Setter Property="Foreground" Value="#FF027DB8"/>
        <Setter Property="Background" Value="#FFD2D5D8"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Maximum" Value="100"/>
        <Setter Property="Value" Value="50"/>
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="BorderBrush">
            <Setter.Value>
                <LinearGradientBrush EndPoint=".5,1" StartPoint=".5,0">
                    <GradientStop Color="#FFAEB7BF" Offset="0"/>
                    <GradientStop Color="#FF919EA7" Offset="0.35"/>
                    <GradientStop Color="#FF7A8A99" Offset="0.35"/>
                    <GradientStop Color="#FF647480" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ProgressBar">
                    <Grid x:Name="Root">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Determinate"/>
                                <VisualState x:Name="Indeterminate">
                                    <Storyboard RepeatBehavior="Forever">
                                        <ObjectAnimationUsingKeyFrames Duration="00:00:00" Storyboard.TargetName="IndeterminateRoot" Storyboard.TargetProperty="(UIElement.Visibility)">
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Duration="00:00:00" Storyboard.TargetName="DeterminateRoot" Storyboard.TargetProperty="(UIElement.Visibility)">
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Collapsed</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ProgressBarTrack" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3" ToolTipService.ToolTip="{TemplateBinding Maximum}">
                            <Border.Effect>
                                <DropShadowEffect Color="#FF626262" Opacity="0.8"/>
                            </Border.Effect>
                            <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Text="{TemplateBinding Maximum}" TextWrapping="NoWrap" Foreground="White" Margin="0,0,2,0"/>
                        </Border>
                        <Grid x:Name="ProgressBarRootGrid">
                            <Rectangle x:Name="ProgressBarRootGradient" RadiusX="1.5" RadiusY="1.5" Margin="{TemplateBinding BorderThickness}" Canvas.ZIndex="1" Stroke="Black">
                                <Rectangle.Fill>
                                    <LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
                                        <GradientStop Color="#B2FFFFFF" Offset="0"/>
                                        <GradientStop Color="#0CFFFFFF" Offset="0.198"/>
                                        <GradientStop Color="#91FFFFFF" Offset="0.336"/>
                                        <GradientStop Color="#0CFFFFFF" Offset="1"/>
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <Grid x:Name="IndeterminateRoot" Visibility="Collapsed">
                                <Rectangle x:Name="IndeterminateSolidFill" Fill="{TemplateBinding Foreground}" Stroke="#FF448DCA" StrokeThickness="0" RadiusX="2" RadiusY="2" Margin="{TemplateBinding BorderThickness}" Opacity="1" RenderTransformOrigin="0.5,0.5"/>
                            </Grid>
                            <Grid x:Name="DeterminateRoot" Margin="1">
                                <Grid x:Name="ProgressBarIndicator" Margin="0" HorizontalAlignment="Left" ToolTipService.ToolTip="{TemplateBinding Value}">
                                    <Rectangle x:Name="ProgressBarIndicator1" Fill="{TemplateBinding Foreground}" StrokeThickness="0" RadiusX="1.5" RadiusY="1.5" Margin="0"/>
                                    <TextBlock HorizontalAlignment="Right" Margin="0,0,2,0" VerticalAlignment="Center" TextWrapping="Wrap" Foreground="White" Text="{TemplateBinding Value}"/>
                                </Grid>
                            </Grid>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我通过本地UserControl.Resources引用它,如下所示:

        <ProgressBar x:Name="availableHoursProgress"
                 Margin="2" 
                 Foreground="#FF42B802" 
                 Background="Red" 
                 Style="{StaticResource ProgressBarStyle1}" 
                 BorderThickness="0" 
                 BorderBrush="{x:Null}" 
                 Value="2" 
                 Maximum="7.5"
                 HorizontalAlignment="Stretch"
                 />

提前感谢您提供的任何帮助,

标记

====更新====

好的,这个问题是因为我绑定的对象类型为 Double ,而 Textblock.Text 期望字符串

所以我的新问题是如何将转换器应用于TemplateBinding,或者在ProgressBar上公开字符串属性:

public string ValueString{ get { return Value.ToString(); } }

1 个答案:

答案 0 :(得分:0)

这个问题已经回答here

好工作詹姆斯!!