views:

167

answers:

2

I was playing with progress bar customization in Silverlight application. What I want to reach is to have progress bar label to show current its state in the following format: "Value / Maximum". So, user will see what is the current value, and what is the maximum possible value. Here is a style for progress bar I use:

<Style x:Key="ProgressBarStyle" TargetType="ProgressBar">
        <Setter Property="Width" Value="97.21" />
        <Setter Property="Height" Value="19" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ProgressBar">
                    <Canvas x:Name="LevelField" Width="99" Height="21">
                        ...
                    <TextBlock ... DataContext="{TemplateBinding Value}" Text="{Binding Converter={StaticResource DecNumberToStringConverter}}"/>
                    </Canvas>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

The way I want to implement this, is to have a value converter, that will convert current value and maximum possible into the proper string. It does work properly, if it is written like above. However, I also need to provide ConverterParameter for Convertor, but not sure how to make it. When I write like this: , ConvertParameter={Binding Maximum}, it shows error on start, that Text attribute is not found in TextBlock. I was also trying to set DataContext as {RelativeSource Self}, but then it didn't displays error that DataContext attribute is not found.

How to make the described progress bar label properly?

A: 

Okay, seems like I found one of ways. It is not the best one, but works if you don't need a universal solution. So, all I do is create more than 1 progress bar styles, and then in each type of progress bar, specify its type as a string parameter to converter.

Then in converter (this one remains the same for all progress bars) I examine parameter, and specify myself what is the maximum number for that toolbar, using a constant, or direct request for progress bar's Maximum value.

Though, still would love to hear a universal solution.

Alexander K
A: 

Try binding the TextBox to the parent and then your value converter can access both the Value and the Maximum. {Binding RelativeSource={RelativeSource TemplatedParent}}.

Then your value converter can cast the object as a ProgressBar and use the values directly.

Stephan