用于加载窗口的progess bar或其他想法

时间:2017-05-24 21:56:20

标签: c# wpf datagrid progress-bar

我有一个Window,在显示其内容之前需要大约4-5秒才能加载。 正在运行一个大型查询来填充DataGrid。 在加载期间,新的Window会打开,但DataGrid部分会在短时间内变黑。

如何添加ProgressBar或其他内容,以便用户不会认为应用程序崩溃?

public partial class SqlCreatedReports : Window
{
    public SqlCreatedReports()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DataGridFill();        
    }

    private void DataGridFill()
    {
        CollectionViewSource dataViewSource =
            (CollectionViewSource)(this.FindResource("dataViewSource"));
        // Load data by setting the CollectionViewSource.Source property:
        // dataViewSource.Source = [generic data source]

        var manager = new ReportMadeManager();

        if (Convert.ToInt32(statusReportID.Content) == 4)
        {
            dataGridReports.ItemsSource = manager.selectReportDJVJ(
                Convert.ToInt32(statusParameter1.Content),
                Convert.ToInt32(statusParameter2.Content));
        }
        else if (Convert.ToInt32(statusReportID.Content)==5)
        {
            dataGridReports.ItemsSource = manager.selectReportCriticalProducts(
                Convert.ToInt32(statusParameter1.Content),
                Convert.ToInt32(statusParameter2.Content));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

将NuGet引用添加到Extended.Wpf.Toolkit

使用DataGrid属性BusyIndicator围绕IsBusyDataGridFill()。 让<Window x:Class="StackOverflow_ProgressBar.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:StackOverflow_ProgressBar" xmlns:tlkt="http://schemas.xceed.com/wpf/xaml/toolkit" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <tlkt:BusyIndicator Name="DataGridBusyIndicator"> <DataGrid Name="dataGridReports" Loaded="dataGridReports_Loaded"/> </tlkt:BusyIndicator> </Grid> </Window> 异步等待繁重的任务。

的Xaml:

private void dataGridReports_Loaded(object sender, RoutedEventArgs e)
{
    DataGridFill();
}

private async void DataGridFill()
{
    DataGridBusyIndicator.IsBusy = true;

    try
    {
        var items = await Task.Run(() =>
        {
            var manager = new ReportMadeManager();

            if (Convert.ToInt32(statusReportID.Content) == 4)
            {
                return manager.selectReportDJVJ(
                    Convert.ToInt32(statusParameter1.Content),
                    Convert.ToInt32(statusParameter2.Content));
            }
            else if (Convert.ToInt32(statusReportID.Content) == 5)
            {
                return manager.selectReportCriticalProducts(
                    Convert.ToInt32(statusParameter1.Content),
                    Convert.ToInt32(statusParameter2.Content));
            }
        });

        dataGridReports.ItemsSource = items;
    }
    finally
    {
        DataGridBusyIndicator.IsBusy = false;
    }
}

C#中的MainWindow:

classes.dex