Ali Tor
Ali Tor

Reputation: 2945

How to bind the ProgressBar Value to Textbox in ResourceDictionary?

I have a custom ProgressBar control in XAML and I want to set a TextBox over it to write the ProgressBar Value. But I can't bind the Value correctly.

The code part to be fixed:

<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="ValueChanged">
    <BeginStoryboard>
        <Storyboard>
            <ObjectAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetName="txt"
                                                              Storyboard.TargetProperty="Text">
                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{Binding  Path=Value,RelativeSource={RelativeSource TemplatedParent}, StringFormat={}{0:0}%}"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>

UPDATED: Whole code in style.xaml (ResourceDictionary)

<Style x:Key="colorizedPB" TargetType="ProgressBar">
<Setter Property="Background" Value="Red"/>
<Setter Property="BorderBrush" Value="LightGray"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="ProgressBar">
            <Viewbox>
                <Border HorizontalAlignment="Left" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
                            BorderBrush="Gray" BorderThickness="1">
                    <Grid>
                        <Rectangle x:Name="rect" HorizontalAlignment="Left" Fill="{TemplateBinding Background}" Width="0" Height="{TemplateBinding Height}">
                            <Rectangle.RenderTransform>
                                <TranslateTransform X="0" Y="0"/>
                            </Rectangle.RenderTransform>
                        </Rectangle>
                        <TextBlock x:Name="txt" FontSize="13" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center" Text="test"/>
                    </Grid>
                </Border>
            </Viewbox>
            <ControlTemplate.Triggers>
                <EventTrigger RoutedEvent="ValueChanged">
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetName="txt"
                                                              Storyboard.TargetProperty="Text">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ProgressBar}}, Path=Value, Mode=OneWay}"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

In MainWindow.xaml

<Grid Grid.Column="1" Margin="8,102,10,53">
            <ProgressBar Background="LightBlue" Name="progressBar" Style="{DynamicResource colorizedPB}" Width="200" Height="20"/>
        </Grid>

Upvotes: 0

Views: 432

Answers (1)

Delete the EventTrigger and try this:

<TextBlock 
    x:Name="txt" 
    FontSize="13" 
    Foreground="{TemplateBinding Foreground}" 
    HorizontalAlignment="Center" 
    VerticalAlignment="Center" 
    Text="{TemplateBinding Value}"
    />

You may want to format Value, in which case you'll need to use a Binding instead:

<TextBlock 
    x:Name="txt" 
    FontSize="13" 
    Foreground="{TemplateBinding Foreground}" 
    HorizontalAlignment="Center" 
    VerticalAlignment="Center" 
    Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, StringFormat={}{0:0.00}}"
    />

Upvotes: 1

Related Questions