简体   繁体   English

如何在进度条控件C#中显示字符串

[英]How to Display a String in a Progress Bar Control C#

I am using C# and WPF for my GUI. 我在GUI中使用C#和WPF。 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. 我希望每200ms显示1个字符,因此该字符串将自动完成动画。 How can I display a string in the progress bar in WPF and how can I animate it's display? 如何在WPF的进度栏中显示字符串,以及如何对其显示进行动画处理?

Have you tried the Text property? 您是否尝试过Text属性?

MSDN 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). 您可以将ProgressBar子类化,添加Text依赖项属性,然后为其创建一个模板,该模板将使用转换器将绑定到Text属性的TextBox包含在内,该转换器将基于ProgressBarValue返回一个子字符串(或者您可以处理它在课程本身中)。

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. 如果您弄乱了ProgressBar的值,则将在渐变画笔中的透明和不透明之间移动过渡。

Subscribe to ProgressChanged but don't use a ProgressBar. 订阅ProgressChanged,但不要使用ProgressBar。 Just update a TextBox with new text. 只需使用新文本更新TextBox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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