Custom ProgressBarBrushConverter Not Filling In ProgressBar

Posted by Wonko the Sane on Stack Overflow See other posts from Stack Overflow or by Wonko the Sane
Published on 2011-01-06T18:50:05Z Indexed on 2011/01/06 18:53 UTC
Read the original article Hit count: 483

Filed under:
|
|

Hello All,

I am attempting to create a custom ProgressBarBrushConverter, based on information from here and here. However, when it runs, the progress is not being shown. If I use the code found in the links, it appears to work correctly.

Here is the converter in question:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    ProgressBar progressBar = null;

    foreach (object value in values)
    {
        if (value is ProgressBar)
        {
            progressBar = value as ProgressBar;
            break;
        }
    }

    if (progressBar == null)
        return DependencyProperty.UnsetValue;

    FrameworkElement indicator = 
        progressBar.Template.FindName("PART_Indicator", progressBar) 
            as FrameworkElement;

    DrawingBrush drawingBrush = new DrawingBrush();
    drawingBrush.Viewport = drawingBrush.Viewbox = 
        new Rect(0.0, 0.0, indicator.ActualWidth, indicator.ActualHeight);
    drawingBrush.ViewportUnits = BrushMappingMode.Absolute;
    drawingBrush.TileMode = TileMode.None;
    drawingBrush.Stretch = Stretch.None;

    DrawingGroup group = new DrawingGroup();
    DrawingContext context = group.Open();

    context.DrawRectangle(progressBar.Foreground, null,
        new Rect(0.0, 0.0, indicator.ActualWidth, indicator.ActualHeight));

    context.Close();
    drawingBrush.Drawing = group;
    return drawingBrush;
}

Here is the ControlTemplate (the MultiBinding is to make sure that the converter is called whenever the Value or IsIndeterminate properties are changed):

<ControlTemplate x:Key="customProgressBarTemplate"
                 TargetType="{x:Type ProgressBar}">
    <Grid>
        <Path x:Name="PART_Track"
              HorizontalAlignment="Left"
              VerticalAlignment="Top"
              Stretch="Fill" 
              StrokeLineJoin="Round"                   
              Stroke="#DDCBCBCB" 
              StrokeThickness="4" 
              Data="M 20,100 L 80,10 C 100,120 160,140 190,180 S 160,220 130,180 T 120,150 20,100 Z ">
            <Path.Fill>
                <MultiBinding>
                    <MultiBinding.Converter>
                        <local:ProgressBarBrushConverter />
                    </MultiBinding.Converter>

                    <Binding RelativeSource="{RelativeSource FindAncestor, 
                        AncestorType={x:Type ProgressBar}}" />                      
                    <Binding Path="IsIndeterminate" 
                                 RelativeSource="{RelativeSource TemplatedParent}"/>
                    <Binding Path="Value"
                                 RelativeSource="{RelativeSource TemplatedParent}"/>
                </MultiBinding>
            </Path.Fill>
            <!--<Path.LayoutTransform>
                <RotateTransform Angle="180" CenterX="190" CenterY="110" />
            </Path.LayoutTransform>-->
        </Path>

        <Rectangle x:Name="PART_Indicator" 
                   HorizontalAlignment="Stretch" 
                   VerticalAlignment="Stretch"                       
                   Margin="1" />
    </Grid>
</ControlTemplate>

Finally, the Window code (fairly straightforward - it just animates progress from 0 to 100 and back again):

<ProgressBar x:Name="progress"
             Template="{StaticResource customProgressBarTemplate}"
             Foreground="Red">
    <ProgressBar.Triggers>
        <EventTrigger RoutedEvent="ProgressBar.Loaded">
            <BeginStoryboard x:Name="storyAnimate">
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames 
                                     Duration="0:0:12"
                                     AutoReverse="True"
                                     FillBehavior="Stop"
                                     RepeatBehavior="Forever"
                                     Storyboard.TargetName="progress"
                                     Storyboard.TargetProperty="(ProgressBar.Value)">
                        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0" />
                        <LinearDoubleKeyFrame Value="100" KeyTime="0:0:5" />
                        <LinearDoubleKeyFrame Value="100" KeyTime="0:0:6" />
                        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:11" />
                        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:12" />
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </ProgressBar.Triggers>
</ProgressBar>

I am thinking that the problem is in the DrawRectangle call in the Convert method, but setting a TracePoint on it shows what appear to be valid values for the Rect.

What am I missing here?

Thanks, wTs

© Stack Overflow or respective owner

Related posts about wpf

Related posts about progressbar