C# WPF 进度条,根据读取数据显示进度条进度,根据Excel文件读取数据,进度条样式

时间:2021-10-20 02:09:01

后台代码:

 //导入
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 = ; UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(probar1.SetValue);
for (int i = ; i < cout; i++)
{
UList.Add(new ExcelModel() { Code = data1.Rows[i][].ToString(), DName = data1.Rows[i][].ToString(), Name = data1.Rows[i][].ToString(), Company = data1.Rows[i][].ToString() }); Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, Convert.ToDouble(i + ) }); if (cout <= )
{
double db = Math.Ceiling(100.00 / cout);
var df = (db * cout) - ;
this.lable1.Content = (((i + ) * db) - df) + "%";
}
else if (cout > )
{
double db = 100.00 / cout;
if (Math.Floor(((cout - ) * db) + ) == )
{
this.lable1.Content = Math.Floor((i * db) + ) + "%";
}
else if (Math.Ceiling(((cout - ) * db) + ) == )
{
this.lable1.Content = Math.Ceiling((i * db) + ) + "%";
}
} Thread.Sleep(); }
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 = ; i < ; i++)
{
Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, Convert.ToDouble(i + ) });
this.lable1.Content = (i + ) + "%";
Thread.Sleep();
}
}
#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>