Wpf 使用ProgressBar当前值附近的文本标签创建ProgressBar

Wpf 使用ProgressBar当前值附近的文本标签创建ProgressBar,wpf,progress-bar,Wpf,Progress Bar,可能标题有点不清楚,请看这张图片: 或: 如何创建它?您可以定义进度条的ValueChanged事件并将该值设置为弹出窗口。参考以下示例: //Define a popup in your code, add a Label to it; the below code could be added to the //ctor/any other block which would be invoked prior to the ValueChanged event. //Please e

可能标题有点不清楚,请看这张图片:

或:


如何创建它?

您可以定义进度条的ValueChanged事件并将该值设置为弹出窗口。参考以下示例:

//Define a popup in your code, add a Label to it; the below code could be added to the 
//ctor/any other block which would be invoked prior to the ValueChanged event. 
//Please ensure Popup is declared such that it is visble/accessible in any of the 
//functions - in this case, the ValueChanged Event & where you decide to add label & 
//placement values to it.

Popup pop = new Popup();
Label lbl = new Label();
pop.Child = lbl;
pop.Placement = placementMode.Relative;
pop.PlacementTarget = progressBar;//assuming name of ProgressBar is progressbar

private void ProgressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    pop.IsOpen = true;
    //Set the horizontal offset of Popup in accordance to dimensions of your progress bar
    (pop.Child as Label).Content = progressBar.Value.ToString();
    Thread.Sleep(100);
    pop.IsOpen = false;
}
//在代码中定义一个弹出窗口,添加一个标签;可以将以下代码添加到
//ctor/在ValueChanged事件之前调用的任何其他块。
//请确保弹出窗口的声明使其在任何
//函数-在这种情况下,ValueChanged事件&您决定在其中添加标签&
//它的放置值。
Popup pop=新的Popup();
标签lbl=新标签();
pop.Child=lbl;
pop.Placement=placementMode.Relative;
pop.PlacementTarget=进度条//假设ProgressBar的名称是ProgressBar
私有void ProgressBar_值已更改(对象发送方,RoutedPropertyChangedEventArgs e)
{
pop.IsOpen=真;
//根据进度条的尺寸设置弹出窗口的水平偏移
(pop.Child作为标签).Content=progressBar.Value.ToString();
睡眠(100);
pop.IsOpen=false;
}

注意:如果您需要按照屏幕截图显示弹出窗口的外观和感觉,则必须修改标签模板。

我正在通过计时器更改进度条,但我看不到弹出窗口。是否希望在计时器启动后弹出窗口保持打开状态?如果是,则注释掉pop.IsOpen=false。