简体   繁体   中英

How to Display a String in a Progress Bar Control C#

I am using C# and WPF for my GUI. My goal is to display a string in the progress bar that I get from a text box. I wish to display 1 character every 200ms so thtat the string animates to completion. How can I display a string in the progress bar in WPF and how can I animate it's display?

Have you tried the Text property?

MSDN

You could subclass the ProgressBar , add a Text dependency property and then create a template for it that will include a TextBox bound to the Text property using a converter that will return a substring based on the Value of the ProgressBar (or you could just handle it within the class itself).

Alternatively, there are various ways you could fake it by overlaying a rectangle with a gradient brush over a textblock. The brush should go from transparent to opaque whatever your background color is, and the position of the gradient is tied to the progess bar's value.

Here's a real quick and dirty example of faking it:

<Grid>
    <ProgressBar Name="MyBar" Minimum="0" Maximum="1" Value="0.6">

    </ProgressBar>

    <Grid>
        <TextBlock>Some Text that will be covered depending on the value of the progress bar</TextBlock>
        <Rectangle>
            <Rectangle.Fill>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                    <GradientStop Offset="0" Color="Transparent"/>
                    <GradientStop Offset="{Binding ElementName=MyBar, Path=Value}" Color="Transparent"/>
                    <GradientStop Offset="{Binding ElementName=MyBar, Path=Value}" Color="White"/>
                    <GradientStop Offset="1" Color="White"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</Grid>

If you mess with the value of the ProgressBar, you'll move the transition between transparent and opaque in the gradient brush.

Subscribe to ProgressChanged but don't use a ProgressBar. Just update a TextBox with new text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM