tags:

views:

52

answers:

1

I've create a control template for a progress bar that includes a textblock where I want to put the update the text based on the % of a file downloaded.

I've no problem getting the % etc. I just want to know how from the c# code do I target the textblock.

Here is my controltemplate

<Style TargetType="{x:Type ProgressBar}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ProgressBar}">
                <Grid>
                    <Rectangle Stroke="#FF000000" RenderTransformOrigin="0.5,0.5" x:Name="PART_Track" RadiusX="5" RadiusY="50">
                        <Rectangle.Fill>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="White" Offset="0" />
                                <GradientStop Color="Gray" Offset="1"/>
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>

                    <Rectangle Stroke="#FF000000" RenderTransformOrigin="0.5,0.5" x:Name="PART_Indicator" HorizontalAlignment="Left" RadiusX="5" RadiusY="50">
                        <Rectangle.Fill>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FF023501" Offset="1.0"/>
                                <GradientStop Color="#FFB6F9B4" Offset="0"/>
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                    <Viewbox>
                        <TextBlock TextAlignment="Center" Background="Transparent" FontFamily="Times" Foreground="Black" x:Name="ProgressText" Margin="2,2,2,2">Test
                        </TextBlock>
                    </Viewbox>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Looks like on my screen it's not showing all that code for some reason, anyway, I'm trying to target that TextBlock x:Name "ProgressText"

+1  A: 

Did you try to bind the text to the combobox value? Kind of

<TextBlock TextAlignment="Center" Foreground="Black" x:Name="ProgressText"
           Margin="2,2,2,2"
           Text={TemplateBinding Value,
                                 Converter={StaticResource DoubleToPercent}}/>

You'll need to specify a converter, of course, but as proof of concept you can try witout it.

Edit:
You'll have to employ a multibinding, since your percent value depends on Minimum and Maximum as well:

<TextBlock TextAlignment="Center" Foreground="Black" x:Name="ProgressText"
           Margin="2,2,2,2">
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource DoubleToPercent}">
            <TemplateBinding Path="Value" />
            <TemplateBinding Path="Minimum" />
            <TemplateBinding Path="Maximum" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

Disclaimer: I didn't try this code.

Vlad
That's the way to do it, but I think you might also need to bind to the `Maximum` as a `ConverterParameters` to be able to calculate a percentage
Steve Greatrex
@Steve: I've seen your comment at the moment I finished expanding the answer. Thanks for the hint anyway!
Vlad