rogressBar值绑定到ResourceDictionary

rogressBar值绑定到ResourceDictionary

本文介绍了如何将ProgressBar值绑定到ResourceDictionary中的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XAML中有一个自定义的ProgressBar控件,我想在其上设置一个TextBox来写入ProgressBar值.但是我无法正确绑定值.

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.

要修复的代码部分:

<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>

已更新: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>

在MainWindow.xaml中

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

推荐答案

删除EventTrigger并尝试以下操作:

Delete the EventTrigger and try this:

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

您可能需要设置Value的格式,在这种情况下,您将需要使用Binding代替:

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}}"
    />

这篇关于如何将ProgressBar值绑定到ResourceDictionary中的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:45