WPF 进度条(ProgressBar)示例一

本文讲述:WPF 进度条(ProgressBar)简单的样式修改和使用。

进度显示界面:使用UserControl把ProgressBar和进度值以及要显示的内容全部组装在UserControl界面中,方便其他界面直接进行使用。

<UserControl x:Class="DefProcessBarDemo.DefProcessBar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:DefProcessBarDemo"
             mc:Ignorable="d"
             x:Name="MyWatingViewControl">
    <UserControl.Background>
        <VisualBrush>
            <VisualBrush.Visual>
                <Border x:Name="ControlBackground"
                        Background="Black"
                        Opacity="0.45" />
            </VisualBrush.Visual>
        </VisualBrush>
    </UserControl.Background>
    <Viewbox x:Name="myViewBox"
             Stretch="UniformToFill"
             StretchDirection="DownOnly"
             UseLayoutRounding="True">
        <Grid Margin="0 0 0 0"
              HorizontalAlignment="Center"
              VerticalAlignment="Center"
              MouseDown="Image_MouseDown">
            <Border CornerRadius="5"
                    SnapsToDevicePixels="True">
                <Border.Effect>
                    <DropShadowEffect Color="#000000"
                                      BlurRadius="10"
                                      ShadowDepth="3"
                                      Opacity="0.35"
                                      Direction="270" />
                </Border.Effect>
                <Border Background="#4a4a4a"
                        CornerRadius="5"
                        Margin="5"
                        BorderBrush="#9196a0"
                        BorderThickness="1"
                        SnapsToDevicePixels="True">
                    <Grid Width="500"
                          Height="150">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="35" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="30" />
                        </Grid.RowDefinitions>

                        <Image Name="CloseIco"
                               Width="25"
                               Height="25"
                               Margin="0,0,0,0"
                               MouseDown="Image_MouseDown"
                               HorizontalAlignment="Right"
                               VerticalAlignment="Top" />

                        <StackPanel Grid.Row="1"
                                    Orientation="Horizontal"
                                    HorizontalAlignment="Center">
                            <TextBlock Text="{Binding Message,ElementName=MyWatingViewControl}"
                                       FontSize="18"
                                       Foreground="Yellow"
                                       TextWrapping="WrapWithOverflow"
                                       TextTrimming="CharacterEllipsis"
                                       MaxWidth="450"
                                       VerticalAlignment="Bottom" />
                            <TextBlock Text="("
                                       FontSize="18"
                                       Foreground="Yellow"
                                       VerticalAlignment="Bottom" />
                            <TextBlock Text="{Binding ElementName=progressBar, Path=Value, StringFormat={}{0:0}%}"
                                       FontSize="18"
                                       Foreground="Yellow"
                                       FontFamily="楷体"
                                       VerticalAlignment="Bottom" />
                            <TextBlock Text=")"
                                       FontSize="18"
                                       Foreground="Yellow"
                                       VerticalAlignment="Bottom" />
                        </StackPanel>
                        <Grid  Grid.Row="2"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Top"
                               Margin="0 10">
                            <ProgressBar x:Name="progressBar"
                                         Maximum="100"
                                         Height="25"
                                         Width="420"
                                         Foreground="Green"
                                         Background="LightGray"
                                         HorizontalContentAlignment="Center"
                                         VerticalContentAlignment="Center"
                                         Value="{Binding ProcessBarValue,ElementName=MyWatingViewControl}" />

                        </Grid>
                    </Grid>
                </Border>
            </Border>
        </Grid>
    </Viewbox>
</UserControl>

进度显示界面:UserControl 后台逻辑实现,主要定义了进度值、显示的文本、以及UserControl的大小。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DefProcessBarDemo
{
    /// <summary>
    /// DefProcessBar.xaml 的交互逻辑
    /// </summary>
    public partial class DefProcessBar : UserControl
    {
        public DefProcessBar()
        {
            InitializeComponent();
            this.Loaded += WaitingView_Loaded;
        }

        void WaitingView_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.Parent != null)
            {
                var root = (FrameworkElement)this.Parent;
                if (root != null)
                {
                    this.Width = root.ActualWidth;
                    this.Height = root.ActualHeight;
                    ControlBackground.Width = root.ActualWidth;
                    ControlBackground.Height = root.ActualHeight;
                }
            }
        }
        #region Property

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(DefProcessBar),
            new PropertyMetadata(""));

        public double ProcessBarValue
        {
            get { return (double)GetValue(ProcessBarValueProperty); }
            set { SetValue(ProcessBarValueProperty, value); }
        }

        public static readonly DependencyProperty ProcessBarValueProperty = DependencyProperty.Register("ProcessBarValue", typeof(double), typeof(DefProcessBar),
            new PropertyMetadata(0.0));

        #endregion

        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
        }
    }
}

 在MainWindow界面中调用[进度显示界面],示例如下:

<Window x:Class="DefProcessBarDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DefProcessBarDemo"
        mc:Ignorable="d" Title="DefProcessBar" Width="600" Height="500"
        WindowStartupLocation="CenterScreen" x:Name="mainwnd"
        xmlns:pdb="clr-namespace:DefProcessBarDemo" Background="Teal"
        >
    <StackPanel>
        <Button Height="30" Width="120" Margin="20" Content="点击" Click="Button_Click"/>
        <pdb:DefProcessBar VerticalAlignment="Center"
                           ProcessBarValue="{Binding ExportValue, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                           Message="{Binding ExportMessage, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />        
    </StackPanel>
</Window>

 后台模拟进度变化,使用Task任务,更新进度值,代码示例如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DefProcessBarDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, System.ComponentModel.INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private string m_ExportMessage = "正在导出,请稍后....";
        /// <summary>
        /// 
        /// <summary>
        public string ExportMessage
        {
            get { return m_ExportMessage; }
            set
            {
                m_ExportMessage = value;
                OnPropertyChanged("ExportMessage");
            }
        }

        private double m_ExportValue = 0.0;
        /// <summary>
        /// 
        /// <summary>
        public double ExportValue
        {
            get { return m_ExportValue; }
            set
            {
                m_ExportValue = value;
                OnPropertyChanged("ExportValue");
            }
        }

        #region MyRegion
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                for(int i = 1; i < 101; i++)
                {
                    ExportValue++;
                    System.Threading.Thread.Sleep(1000);
                    if (ExportValue == 100)
                        ExportMessage = "完成";

                }
            });
            string strRes = "";
            bool bRet = GetCmdResult("netsh wlan show profiles", out strRes);
        }
    }
}

运行时,点击【点击】按钮,即可看到进度持续不断地更新,界面如下图所示:

 



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/966121.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

LabVIEW自定义测量参数怎么设置?

以下通过一个温度采集案例&#xff0c;说明在 LabVIEW 中设置自定义测量参数的具体方法&#xff1a; 案例背景 ​ 假设使用 NI USB-6009 数据采集卡 和 热电偶传感器 监测温度&#xff0c;需自定义以下参数&#xff1a; 采样率&#xff1a;1 kHz 输入量程&#xff1a;0~10 V&a…

新能源产业的质量革命:六西格玛培训如何重塑制造竞争力

在新能源行业狂飙突进的今天&#xff0c;企业若想在全球供应链中占据高地&#xff0c;仅靠技术突破已远远不够。制造效率的毫厘之差&#xff0c;可能成为市场话语权的千里之距。某光伏巨头曾因电池片良率低于行业均值1.5%&#xff0c;导致年损失超2.3亿元——这恰恰印证了六西格…

(11)gdb 笔记(4):设置执行方向 set exec-direction,

&#xff08;28&#xff09;引入 record 后&#xff0c;可以 设置执行方向 set exec-direction &#xff1a; 实践&#xff1a; &#xff08;29&#xff09; &#xff08;33&#xff09; 谢谢

redis持久化理论

0 前言 什么是持久化 redis操作都是在内存中&#xff0c;如果出现宕机的话&#xff0c;数据将不复存在&#xff0c;所以持久化是将内存中的数据刷盘到磁盘中&#xff0c;redis可以提供RDB和AOF将数据写入磁盘中。 一 持久化技术 本章节将介绍持久化RDB和AOF两个技术&#xf…

25/2/7 <机器人基础>雅可比矩阵计算 雅可比伪逆

雅可比矩阵计算 雅可比矩阵的定义 假设我们有一个简单的两个关节的平面机器人臂&#xff0c;其末端执行器的位置可以表示为&#xff1a; 其中&#xff1a; L1​ 和 L2 是机器人臂的长度。θ1​ 和 θ2是关节的角度。 计算雅可比矩阵 雅可比矩阵 JJ 的定义是将关节速度与末…

鸿蒙UI(ArkUI-方舟UI框架)- 使用文本

返回主章节 → 鸿蒙UI&#xff08;ArkUI-方舟UI框架&#xff09; 文本使用 文本显示 (Text/Span) Text是文本组件&#xff0c;通常用于展示用户视图&#xff0c;如显示文章的文字内容。Span则用于呈现显示行内文本。 创建文本 string字符串 Text("我是一段文本"…

科技赋能数字内容体验的核心技术探索

内容概要 在数字化时代&#xff0c;科技的迅猛发展为我们的生活和工作带来了深刻的变革。数字内容体验已经成为人们获取信息和娱乐的重要途径&#xff0c;而这背后的技术支持则扮演着至关重要的角色。尤其是在人工智能、虚拟现实和区块链等新兴技术的推动下&#xff0c;数字内…

详细教程 | 如何使用DolphinScheduler调度Flink实时任务

Apache DolphinScheduler 非常适用于实时数据处理场景&#xff0c;尤其是与 Apache Flink 的集成。DolphinScheduler 提供了丰富的功能&#xff0c;包括任务依赖管理、动态调度、实时监控和日志管理&#xff0c;能够有效简化 Flink 实时任务的管理和部署。通过 DolphinSchedule…

棋盘(二维差分)

题目&#xff1a; 5396. 棋盘 题目 提交记录 讨论 题解 视频讲解 小蓝拥有 nnnn 大小的棋盘&#xff0c;一开始棋盘上全都是白子。 小蓝进行了 mm 次操作&#xff0c;每次操作会将棋盘上某个范围内的所有棋子的颜色取反(也就是白色棋子变为黑色&#xff0c;黑色棋子变…

MySQL数据库基础(创建/删除 数据库/表)

一、数据库的操作 1.1 显示当前数据库 语法&#xff1a;show databases&#xff1b; <1>show 是一个关键字&#xff0c;表示要执行的操作类型 <2>databases 是复数&#xff0c;表示显示所有数据库 上面的数据库中&#xff0c;除了java113&#xff0c;其它的数据库…

【WebLogic】Oracle发布WebLogic 14c最新版本-14.1.2.0

根据Oracle官方产品经理的博客&#xff0c;Oracle于2024年12月20日正式对外发布了WebLogic 14c的第二个正式版本&#xff0c;版本号为 14.1.2.0.0 &#xff0c;目前官方已开放客户端下载。该版本除继续支持 Jakarta EE 8 版本外&#xff0c;还增加了对 Java SE 17&#xff08;J…

SQL Server 数据库迁移到 MySQL 的完整指南

文章目录 引言一、迁移前的准备工作1.1 确定迁移范围1.2 评估兼容性1.3 备份数据 二、迁移工具的选择2.1 使用 MySQL Workbench2.2 使用第三方工具2.3 手动迁移 三、迁移步骤3.1 导出 SQL Server 数据库结构3.2 转换数据类型和语法3.3 导入 MySQL 数据库3.4 迁移数据3.5 迁移存…

springboot+vue导入ruoyi项目的框架

一、介绍 RuoYi-Vue版本&#xff0c;采用了前后端分离的单体架构设计软件环境&#xff1a;JDK、Mysql、Redis、Maven、Node技术选型: Spring Boot、Spring Security、MyBatis、Jwt、Vue3、Element-Plus官方地址: https://gitee.com/y_project/RuoYi-Vue 官方推荐的版本如下&a…

jvm 篇

字节码的作用 ‌跨平台性‌&#xff1a;字节码是Java实现跨平台特性的关键。Java源代码编译成字节码后&#xff0c;可以在任何安装了Java虚拟机&#xff08;JVM&#xff09;的设备上运行&#xff0c;这使得Java应用程序能够在不同的操作系统和硬件平台上运行而无需重新编译。‌…

【Springboot】Springboot 自定义线程池的参数配置最优是多少

博主介绍&#xff1a;✌全网粉丝22W&#xff0c;CSDN博客专家、Java领域优质创作者&#xff0c;掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域✌ 技术范围&#xff1a;SpringBoot、SpringCloud、Vue、SSM、HTML、Nodejs、Python、MySQL、PostgreSQL、大数据、物…

【2】Cisco SD-WAN 组件介绍

1. 概述 Cisco SD-WAN 是一套基于软件定义的广域网(SD-WAN)解决方案,能够提供安全、可扩展且高效的网络连接。它通过集中的控制和智能路径选择,实现跨多个站点的可靠性、可见性和优化。 在 Cisco SD-WAN 体系架构中,主要由四个核心组件构成: vManage(管理平面) vSmart…

测试中的第一性原理:回归本质的质量思维革命

在软件工程领域&#xff0c;测试活动常被惯性思维和经验主义所主导——测试用例库无限膨胀、自动化脚本维护成本居高不下、测试策略与业务目标渐行渐远。要突破这种困境&#xff0c;第一性原理&#xff08;First Principles Thinking&#xff09;提供了独特的解题视角&#xff…

《chatwise:DeepSeek的界面部署》

ChatWise&#xff1a;DeepSeek的界面部署 摘要 本文详细描述了DeepSeek公司针对其核心业务系统进行的界面部署工作。从需求分析到技术实现&#xff0c;再到测试与优化&#xff0c;全面阐述了整个部署过程中的关键步骤和解决方案。通过本文&#xff0c;读者可以深入了解DeepSee…

机器学习在癌症分子亚型分类中的应用

学习笔记&#xff1a;机器学习在癌症分子亚型分类中的应用——Cancer Cell 研究解析 1. 文章基本信息 标题&#xff1a;Classification of non-TCGA cancer samples to TCGA molecular subtypes using machine learning发表期刊&#xff1a;Cancer Cell发表时间&#xff1a;20…

什么是AIOps?

AIOps&#xff08;人工智能运维&#xff0c;Artificial Intelligence for IT Operations&#xff09;是通过使用人工智能&#xff08;AI&#xff09;技术来增强 IT 运维&#xff08;IT Operations&#xff09;的智能化、自动化和效率的概念。它结合了机器学习、数据分析、自动化…