C# WPF 进度条,根据读取数据显示进度条进度,根据Excel文件读取数据,进度条样式
后台代码:
//导入 private void Border_MouseLeftButtonUp_2(object sender, MouseButtonEventArgs e) { var path = this.txtbox1.Text; if (path.Contains(".xlsx") || path.Contains(".xls")) { //显示隐藏的弹框 this.Tborders.Visibility = Visibility.Visible; var data1 = GetDataTable(path); List<ExcelModel> UList = new List<Model.ExcelModel>(); int cout = data1.Rows.Count; probar1.Maximum = cout; probar1.Value = 0; UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(probar1.SetValue); for (int i = 0; i < cout; i++) { UList.Add(new ExcelModel() { Code = data1.Rows[i][0].ToString(), DName = data1.Rows[i][1].ToString(), Name = data1.Rows[i][2].ToString(), Company = data1.Rows[i][3].ToString() }); Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, Convert.ToDouble(i + 1) }); if (cout <= 100) { double db = Math.Ceiling(100.00 / cout); var df = (db * cout) - 100; this.lable1.Content = (((i + 1) * db) - df) + "%"; } else if (cout > 100) { double db = 100.00 / cout; if (Math.Floor(((cout - 1) * db) + 1) == 100) { this.lable1.Content = Math.Floor((i * db) + 1) + "%"; } else if (Math.Ceiling(((cout - 1) * db) + 1) == 100) { this.lable1.Content = Math.Ceiling((i * db) + 1) + "%"; } } Thread.Sleep(10); } this.datagrid1.ItemsSource = UList; //beginImport(100, 0); } else { System.Windows.MessageBox.Show("请选择Excel文件"); } }
进度条后台主要代码:
#region 进度条 private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value); private void beginImport(double max, double v) { probar1.Maximum = max; probar1.Value = v; UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(probar1.SetValue); for (int i = 0; i < 100; i++) { Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, Convert.ToDouble(i + 1) }); this.lable1.Content = (i + 1) + "%"; Thread.Sleep(10); } } #endregion
进度条XAML:
<Border Width="302" Height="15" BorderThickness="1" BorderBrush="#999"> <ProgressBar BorderThickness="0" x:Name="probar1" Width="302" Height="15" Background="#fff" Foreground="#199ED8" Style="{StaticResource SimpleProgressBar}"> </ProgressBar> </Border>
样式:
<Window.Resources> <!--ProgressBar Style--> <Style TargetType="ProgressBar" x:Key="SimpleProgressBar"> <Setter Property="IsTabStop" Value="False" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ProgressBar"> <Grid x:Name="Root" > <Border x:Name="PART_Track" Background="{TemplateBinding Background}" CornerRadius="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> <Border x:Name="PART_Indicator" HorizontalAlignment="Left" Background="{TemplateBinding Foreground}" CornerRadius="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="Orientation" Value="Vertical"> <Setter Property="LayoutTransform" TargetName="Root" > <Setter.Value> <RotateTransform Angle="-90" /> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources>