WPF 中进度条上的文本

发布于 2024-07-04 18:35:25 字数 282 浏览 7 评论 0原文

对于 WPF 行家来说,这可能是理所当然的事情,但我想知道是否有一种简单的方法可以将文本放在 WPF ProgressBar 上。 对我来说,一个空的进度条看起来毫无意义。 这是屏幕空间,可以携带有关正在进行的内容的信息,甚至只是在表示中添加数字。 现在,WPF 都是关于容器和扩展的,我正在慢慢地思考这一点,但由于我没有看到“文本”或“内容”属性,我想我必须添加一些东西到我的进度条容器。 有没有一两种技术比我最初的 WinForms 冲动更自然? 向进度条添加文本的最佳、最自然的 WPF 方式是什么?

This may be a no-brainer for the WPF cognoscenti, but I'd like to know if there's a simple way to put text on the WPF ProgressBar. To me, an empty progress bar looks naked. That's screen real estate that could carry a message about what is in progress, or even just add numbers to the representation. Now, WPF is all about containers and extensions and I'm slowly wrapping my mind around that, but since I don't see a "Text" or "Content" property, I'm thinking I'm going to have to add something to the container that is my progress bar. Is there a technique or two out there that is more natural than my original WinForms impulses will be? What's the best, most WPF-natural way to add text to that progress bar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

冷…雨湿花 2024-07-11 18:35:25

带有文本和 2 个属性绑定的进度条(值/最大值):

<Grid>
    <ProgressBar Name="pbUsrLvl"
                 Minimum="1" 
                 Maximum="99" 
                 Value="59" 
                 Margin="5"  
                 Height="24"  Foreground="#FF62FF7F"/>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock.Text>
            <MultiBinding StringFormat="{}UserLvl:{0}/{1}">
                <Binding Path="Value" ElementName="pbUsrLvl" />
                <Binding Path="Maximum" ElementName="pbUsrLvl" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
</Grid>

结果:

< img src="https://i.sstatic.net/cKsaG.png" alt="在此处输入图像描述">


相同,但进度百分比

<Grid>
    <ProgressBar Name="pbLifePassed"
                 Minimum="0" 
                 Value="59" 
                 Maximum="100"
                 Margin="5" Height="24" Foreground="#FF62FF7F"/>
    <TextBlock Text="{Binding ElementName=pbLifePassed, Path=Value, StringFormat={}{0:0}%}" 
           HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

在此处输入图像描述< /a>

ProgressBar with Text and Binding from 2 Properties ( Value/Maximum value ):

<Grid>
    <ProgressBar Name="pbUsrLvl"
                 Minimum="1" 
                 Maximum="99" 
                 Value="59" 
                 Margin="5"  
                 Height="24"  Foreground="#FF62FF7F"/>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock.Text>
            <MultiBinding StringFormat="{}UserLvl:{0}/{1}">
                <Binding Path="Value" ElementName="pbUsrLvl" />
                <Binding Path="Maximum" ElementName="pbUsrLvl" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
</Grid>

Rezult:

enter image description here


The same but with % of progress :

<Grid>
    <ProgressBar Name="pbLifePassed"
                 Minimum="0" 
                 Value="59" 
                 Maximum="100"
                 Margin="5" Height="24" Foreground="#FF62FF7F"/>
    <TextBlock Text="{Binding ElementName=pbLifePassed, Path=Value, StringFormat={}{0:0}%}" 
           HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

enter image description here

想挽留 2024-07-11 18:35:25

这是基于给出的答案。
由于我使用的是 MahApps Metro,所以我最终得到了以下结果:

<Grid>
    <metro:MetroProgressBar x:Name="pbar" Value="50" Height="20"></metro:MetroProgressBar>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding ElementName=pbar, Path=Value, StringFormat={}{0:0}%}"></TextBlock>
</Grid>

如果您想使用带有 Metro 风格的普通栏:

<Grid>
    <ProgressBar x:Name="pbar" Value="50" Height="20" Style="{StaticResource MetroProgressBar}"></ProgressBar>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding ElementName=pbar, Path=Value, StringFormat={}{0:0}%}"></TextBlock>
</Grid>

没有风格的情况相同:

<Grid>
    <ProgressBar x:Name="pbar" Value="60" Height="20" Style="{x:Null}"></ProgressBar>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding ElementName=pbar, Path=Value, StringFormat={}{0:0}%}"></TextBlock>
</Grid>

发生了什么?

您有进度条,只需在其上放置文本即可。
所以你只要像你想的那样使用你的进度条。
将进度条放在网格中并在其中放置一个文本块。
然后您可以根据需要发送文本或获取当前百分比(即进度条中的值)。

This is based on the given answers.
Since I´m using MahApps Metro, I ended up with this:

<Grid>
    <metro:MetroProgressBar x:Name="pbar" Value="50" Height="20"></metro:MetroProgressBar>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding ElementName=pbar, Path=Value, StringFormat={}{0:0}%}"></TextBlock>
</Grid>

If you want to use the normal bar with Metro Style:

<Grid>
    <ProgressBar x:Name="pbar" Value="50" Height="20" Style="{StaticResource MetroProgressBar}"></ProgressBar>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding ElementName=pbar, Path=Value, StringFormat={}{0:0}%}"></TextBlock>
</Grid>

Same without Style:

<Grid>
    <ProgressBar x:Name="pbar" Value="60" Height="20" Style="{x:Null}"></ProgressBar>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding ElementName=pbar, Path=Value, StringFormat={}{0:0}%}"></TextBlock>
</Grid>

What is Happening?

You have your progressbar and simply just lay text over it.
So you just use your progressbar as you would.
Put the progressbar in a grid and lay an textblock in it.
Then you can text as you wish or grab the current percenteage wich is the value from the progressbar.

下雨或天晴 2024-07-11 18:35:25

右键单击ProgressBar,然后单击编辑模板> 编辑副本。

然后将如下所示的TextBlock放在VS生成的Style中Grid的结束标签上方。

   <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"/>
   <TextBlock Background="Transparent" Text="work in progress" Foreground="Black" TextAlignment="Center"/>
 </Grid>
 <ControlTemplate.Triggers>

Right click ProgressBar, and click Edit Template > Edit a Copy.

Then put the TextBlock as shown below just above the closing tag of Grid in the Style generated by VS.

   <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"/>
   <TextBlock Background="Transparent" Text="work in progress" Foreground="Black" TextAlignment="Center"/>
 </Grid>
 <ControlTemplate.Triggers>
灼疼热情 2024-07-11 18:35:25

一种简单的方法是在进度条后面的文本块中写入您想要的任何内容,并将其左边距更改为负数。

<ProgressBar  BorderBrush="black" Width="250" Height="20" Minimum="0" Maximum="100" Value="{Binding Happiness}"/>

            <TextBlock Margin="-140,0" FontSize="20" Text="{Binding Happiness}" ></TextBlock>
            <TextBlock Margin="-135,0" FontSize="20"  >%</TextBlock>

A simple way to do it is to write anything you want in a textBlock after the progress bar, and change its left margin to a negativ number.

<ProgressBar  BorderBrush="black" Width="250" Height="20" Minimum="0" Maximum="100" Value="{Binding Happiness}"/>

            <TextBlock Margin="-140,0" FontSize="20" Text="{Binding Happiness}" ></TextBlock>
            <TextBlock Margin="-135,0" FontSize="20"  >%</TextBlock>
执手闯天涯 2024-07-11 18:35:25

您可以使用装饰器在其上方显示文本。

请参阅有关 Adorners 的 MSDN 文章

您将创建一个继承自 Adorner 类的类。 重写 OnRender 方法来绘制所需的文本。 如果需要,您可以为自定义装饰器创建一个依赖属性,其中包含要显示的文本。 然后使用我提到的链接中的示例将此装饰器添加到进度栏的装饰器层。

You could use an Adorner to display text over top of it.

See MSDN article on Adorners

You would create a class that inherits from the Adorner class. Override the OnRender method to draw the text that you want. If you want you could create a dependency property for your custom Adorner that contains the text that you want to display. Then use the example in the link I mentioned to add this Adorner to your progress bar's adorner layer.

烟火散人牵绊 2024-07-11 18:35:25

如果您需要可重用的方法来添加文本,则可以创建一个新的 Style/ControlTemplate,其中包含一个附加的 TextBlock 来显示文本。 您可以劫持 TextSearch.Text 附加属性来设置进度栏上的文本。

如果不需要可重用,只需将进度条放在 Grid 中,然后向网格添加 TextBlock 即可。 由于 WPF 可以将元素组合在一起,因此这会很好地工作。

如果需要,您可以创建一个将 ProgressBar 和 TextBlock 公开为公共属性的 UserControl,因此它比创建自定义 ControlTemplate 的工作量要少。

If you are needing to have a reusable method for adding text, you can create a new Style/ControlTemplate that has an additional TextBlock to display the text. You can hijack the TextSearch.Text attached property to set the text on a progress bar.

If it doesn't need to be reusable, simply put the progress bar in a Grid and add a TextBlock to the grid. Since WPF can compose elements together, this will work nicely.

If you want, you can create a UserControl that exposes the ProgressBar and TextBlock as public properties, so it would be less work than creating a custom ControlTemplate.

走过海棠暮 2024-07-11 18:35:25

这可能非常简单(除非有很多方法可以实现这一点)。

您可以使用 Style 来完成此操作,或者只覆盖 TextBlockProgressBar

我个人用它来显示等待完成时的进度百分比。

为了保持简单,我只想有一个Binding
所以我将 TextBock.Text 附加到 ProgressBar.Value

                    sp; 然后只需复制代码即可完成。< /strong>

<Grid>
   <ProgressBar Minimum="0" 
                Maximum="100" 
                Value="{Binding InsertBindingHere}" 
                Name="pbStatus" />
   <TextBlock Text="{Binding ElementName=pbStatus, Path=Value, StringFormat={}{0:0}%}" 
           HorizontalAlignment="Center" 
           VerticalAlignment="Center" />
</Grid>

                     ;                   < strong>这是这样的:

                               
输入图像描述此处

查看 WPF 教程完整的帖子。

This can be very simple (unless there are alot of ways getting this to work).

You could use Style to get this done or you just overlay a TextBlock and a ProgressBar.

I personally use this to show the percentage of the progress when waiting for completion.

To keep it very simple I only wanted to have one Binding only,
so I attached the TextBock.Text to the ProgressBar.Value.

                                           Then just copy the Code to get it done.

<Grid>
   <ProgressBar Minimum="0" 
                Maximum="100" 
                Value="{Binding InsertBindingHere}" 
                Name="pbStatus" />
   <TextBlock Text="{Binding ElementName=pbStatus, Path=Value, StringFormat={}{0:0}%}" 
           HorizontalAlignment="Center" 
           VerticalAlignment="Center" />
</Grid>

                                                Here is how this could look like:

                                   
enter image description here

Check out WPF Tutorial for the full post.

很酷不放纵 2024-07-11 18:35:25

之前的两种响应(创建新的 CustomControlAdorner)都是更好的做法,但如果您只是想要快速和肮脏(或者从视觉上了解如何做到这一点)那么这段代码就可以工作:

<Grid Width="300" Height="50">  
   <ProgressBar Value="50" />
   <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
      My Text
   </TextBlock>
</Grid>

只要记住 z 索引使得列出的最后一项将位于顶部。

另外,如果您还没有 Kaxaml,请务必选择它 - 它非常适合使用当您试图解决问题时使用 XAML。

Both of the prior responses (creating a new CustomControl or an Adorner) are better practices, but if you just want quick and dirty (or to understand visually how to do it) then this code would work:

<Grid Width="300" Height="50">  
   <ProgressBar Value="50" />
   <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
      My Text
   </TextBlock>
</Grid>

Just keep in mind that the z-index is such that the last item listed will be on top.

Also, if you don't have Kaxaml yet, be sure to pick it up - it is great for playing with XAML when you're trying to figure things out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文