中企动力 网站建设 眼镜,网站必须做电子认证吗,牡丹江seo,网站的虚拟人怎么做的本文讲述#xff1a;WPF 进度条(ProgressBar)简单的样式修改和使用。
进度显示界面#xff1a;使用UserControl把ProgressBar和进度值以及要显示的内容全部组装在UserControl界面中#xff0c;方便其他界面直接进行使用。
UserControl x:ClassDefProcessBarDemo…本文讲述WPF 进度条(ProgressBar)简单的样式修改和使用。
进度显示界面使用UserControl把ProgressBar和进度值以及要显示的内容全部组装在UserControl界面中方便其他界面直接进行使用。
UserControl x:ClassDefProcessBarDemo.DefProcessBarxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:localclr-namespace:DefProcessBarDemomc:Ignorabledx:NameMyWatingViewControlUserControl.BackgroundVisualBrushVisualBrush.VisualBorder x:NameControlBackgroundBackgroundBlackOpacity0.45 //VisualBrush.Visual/VisualBrush/UserControl.BackgroundViewbox x:NamemyViewBoxStretchUniformToFillStretchDirectionDownOnlyUseLayoutRoundingTrueGrid Margin0 0 0 0HorizontalAlignmentCenterVerticalAlignmentCenterMouseDownImage_MouseDownBorder CornerRadius5SnapsToDevicePixelsTrueBorder.EffectDropShadowEffect Color#000000BlurRadius10ShadowDepth3Opacity0.35Direction270 //Border.EffectBorder Background#4a4a4aCornerRadius5Margin5BorderBrush#9196a0BorderThickness1SnapsToDevicePixelsTrueGrid Width500Height150Grid.RowDefinitionsRowDefinition Heightauto /RowDefinition Height35 /RowDefinition Height* /RowDefinition Height30 //Grid.RowDefinitionsImage NameCloseIcoWidth25Height25Margin0,0,0,0MouseDownImage_MouseDownHorizontalAlignmentRightVerticalAlignmentTop /StackPanel Grid.Row1OrientationHorizontalHorizontalAlignmentCenterTextBlock Text{Binding Message,ElementNameMyWatingViewControl}FontSize18ForegroundYellowTextWrappingWrapWithOverflowTextTrimmingCharacterEllipsisMaxWidth450VerticalAlignmentBottom /TextBlock Text(FontSize18ForegroundYellowVerticalAlignmentBottom /TextBlock Text{Binding ElementNameprogressBar, PathValue, StringFormat{}{0:0}%}FontSize18ForegroundYellowFontFamily楷体VerticalAlignmentBottom /TextBlock Text)FontSize18ForegroundYellowVerticalAlignmentBottom //StackPanelGrid Grid.Row2HorizontalAlignmentCenterVerticalAlignmentTopMargin0 10ProgressBar x:NameprogressBarMaximum100Height25Width420ForegroundGreenBackgroundLightGrayHorizontalContentAlignmentCenterVerticalContentAlignmentCenterValue{Binding ProcessBarValue,ElementNameMyWatingViewControl} //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 的交互逻辑/// /summarypublic 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 Propertypublic 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));#endregionprivate void Image_MouseDown(object sender, MouseButtonEventArgs e){this.Visibility Visibility.Hidden;}}
}在MainWindow界面中调用[进度显示界面]示例如下
Window x:ClassDefProcessBarDemo.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:DefProcessBarDemomc:Ignorabled TitleDefProcessBar Width600 Height500WindowStartupLocationCenterScreen x:Namemainwndxmlns:pdbclr-namespace:DefProcessBarDemo BackgroundTealStackPanelButton Height30 Width120 Margin20 Content点击 ClickButton_Click/pdb:DefProcessBar VerticalAlignmentCenterProcessBarValue{Binding ExportValue, ModeTwoWay,UpdateSourceTriggerPropertyChanged}Message{Binding ExportMessage, ModeTwoWay, UpdateSourceTriggerPropertyChanged} / /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 的交互逻辑/// /summarypublic partial class MainWindow : Window, System.ComponentModel.INotifyPropertyChanged{public MainWindow(){InitializeComponent();this.DataContext this;}private string m_ExportMessage 正在导出请稍后....;/// summary/// /// summarypublic string ExportMessage{get { return m_ExportMessage; }set{m_ExportMessage value;OnPropertyChanged(ExportMessage);}}private double m_ExportValue 0.0;/// summary/// /// summarypublic double ExportValue{get { return m_ExportValue; }set{m_ExportValue value;OnPropertyChanged(ExportValue);}}#region MyRegionpublic event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(string propertyName){if (PropertyChanged ! null){PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));}}#endregionprivate 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);}}
}运行时点击【点击】按钮即可看到进度持续不断地更新界面如下图所示