如何在自定义Windows Phone控件上添加进度条效果?

时间:2014-04-22 21:37:18

标签: xaml windows-phone expression-blend

这是我的问题:我创建了一个封装“经典按钮”的自定义控件。我出于“业务”原因向控件添加了一些自定义依赖项属性。其中一个属性是“进度”指标值。

当此值更改为X时,我想更改控件背景颜色的X%以产生进度指示器效果。

以下是我拥有的和我想拥有的图片:

Initial State

With progress

这是我的实际代码:

<UserControl x:Name="userControl" x:Class="MyCustomControl"
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"   
Background="White"
         Padding="0"
         Margin="0"
         Width="82"
         Height="87"
     d:designWidth="82"
     d:designHeight="87" SizeChanged="userControl_SizeChanged"    
         >


<UserControl.Resources>
    <Style x:Key="ButtonStyle1" TargetType="Button">
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="grid" Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderThickness)" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Thickness>0,0,0,1</Thickness>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="grid">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Thickness>0,5,0,0</Thickness>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid>

                            <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,0,0,5" CornerRadius="6" Background="{TemplateBinding Background}" >
                                <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>                                
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot">

    <Button x:Name="aButton"
                Style="{StaticResource ButtonStyle1}" 
                Background="{Binding Background, ElementName=userControl}" 
                Foreground="{Binding Foreground, ElementName=userControl}" 
                BorderBrush="{Binding Foreground, ElementName=userControl}"                
                VerticalContentAlignment="Center"
                HorizontalContentAlignment="Center"
                Content="1"
                >

    </Button>

</Grid>

如果需要,我可以添加代码,但它只是一个依赖属性。

我怎样才能在XAML / C#中做到这一点?什么是最好的解决方案?我尝试在名为“ButtonBackground”的“Border”中添加一个矩形,但我找不到如何让它工作......

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

<Grid>
    <Rectangle HorizontalAlignment="Left" Height="159" Margin="174.5,17.5,0,0" Stroke="Black" VerticalAlignment="Top" Width="152" RenderTransformOrigin="0.5,0.5" RadiusY="18.5" RadiusX="18.5">
        <Rectangle.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="-90"/>
                <TranslateTransform/>
            </TransformGroup>
        </Rectangle.RenderTransform>
        <Rectangle.Fill>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="{Binding Value, ElementName=slider}"/>
                <GradientStop Color="White" Offset="{Binding Value, ElementName=slider}"/>
            </LinearGradientBrush>
        </Rectangle.Fill>
    </Rectangle>
    <Slider x:Name="slider" HorizontalAlignment="Left" Margin="172,207,0,0" VerticalAlignment="Top" Width="165" LargeChange="0.1" Maximum="1" TickFrequency="0.1"/>

</Grid>

以下是可以帮助您的代码。我在我的情况下采用矩形,将其背景设置为渐变。作为两个停止的渐变,我将它们设置为两个看起来都是单个填充。我用滑块绑定了两个渐变色块,并自定义了滑块,它的大变化,步进频率等。现在,当我移动滑块时,填充颜色也会根据需要改变。将渐变颜色的两个停止点绑定到您的依赖属性,您将完成它。希望这个解决方案可能适合你。

以下是我所做的一些结果。

Notice the movement of slider Notice again

快乐的编码!

如果您有疑问,请告诉我。