在 WPF 中加载数据时显示 Mahapps.ProgressRing

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32388982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:23:04  来源:igfitidea点击:

Show Mahapps.ProgressRing when loading data in WPF

c#wpfwpf-controlsmahapps.metro

提问by klaydze

I'm populating a DataGridwith sorting and grouping in WPF and I want to implement a progress indicator to the user for them to know that the current query is still running in the background. I'm trying to use the ProgressRingof Mahapps but i don't know how do I implement it. Below is my code.

我正在DataGridWPF 中填充排序和分组,我想为用户实现一个进度指示器,让他们知道当前查询仍在后台运行。我正在尝试使用ProgressRingMahapps 的,但我不知道如何实现它。下面是我的代码。

Code Behind

背后的代码

    void InitSongs()
    {
        this.Dispatcher.Invoke((Action)(() => {
            DataTable dtSong = new DataTable();
            ICollection<Song> songList = new ObservableCollection<Song>();

            using (SqlConnection conn = new SqlConnection(@"Server=.\MSSQL2008R2;Database=MVCDB;Trusted_Connection=True;"))
            {
                string sqlCmd = "SELECT TOP 1000 * FROM SongDb";

                conn.Open();
                using (SqlCommand cmd = new SqlCommand(sqlCmd, conn))
                {
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.CommandText = sqlCmd;
                    cmd.CommandTimeout = 120;
                    dtSong.Load(cmd.ExecuteReader());
                }
            }

            for (int i = 0; i <= dtSong.Rows.Count - 1; i++)
            {
                songList.Add(new Song(Convert.ToInt32(dtSong.Rows[i][0]),
                                        dtSong.Rows[i][1].ToString(),
                                        dtSong.Rows[i][2].ToString(),
                                        Convert.ToInt64(dtSong.Rows[i][3]),
                                        dtSong.Rows[i][4].ToString(),
                                        Convert.ToBoolean(dtSong.Rows[i][5])));
            }

            dgSongs.ItemsSource = songList;

            ICollectionView view = CollectionViewSource.GetDefaultView(dgSongs.ItemsSource);
            view.SortDescriptions.Add(new SortDescription("Artist", ListSortDirection.Ascending));
            //view.SortDescriptions.Add(new SortDescription("Title", ListSortDirection.Ascending));

            view.GroupDescriptions.Add(new PropertyGroupDescription("Artist"));
        }));
    }

    private void btnGetSongs_Click(object sender, RoutedEventArgs e)
    {
        bw.WorkerReportsProgress = true;
        bw.WorkerSupportsCancellation = true;

        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);

        progress1.IsActive = true;
        progress1.Visibility = System.Windows.Visibility.Visible;

        bw.RunWorkerAsync();
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        InitSongs();
    }

    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        progress1.IsActive = false;
        progress1.Visibility = Visibility.Collapsed;
    }

MainWindow.xaml

主窗口.xaml

<Controls:MetroWindow x:Class="PalletTaggingWpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    Title="Pallet Tagging" Height="500" Width="500" ShowIconOnTitleBar="True" 
                  WindowStartupLocation="CenterScreen"
                  GlowBrush="{DynamicResource AccentColorBrush}"
                  BorderBrush="{DynamicResource AccentColorBrush}"
                  EnableDWMDropShadow="True"
                  BorderThickness="1">

<TabControl>
        <TabItem Header="Songs">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>

            <StackPanel>
                <Button Name="btnGetSongs" Content="Get Songs" Click="btnGetSongs_Click"></Button>
            </StackPanel>

            <DataGrid Grid.Row="1" Name="dgSongs" ItemsSource="{Binding}">
                <DataGrid.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Path=Name}" />
                                </StackPanel>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="True">
                                                <Expander.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="{Binding Path=Name}" />
                                                        <TextBlock Text="{Binding Path=ItemCount}"/>
                                                        <TextBlock Text="Items"/>
                                                    </StackPanel>
                                                </Expander.Header>
                                                <ItemsPresenter />
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </DataGrid.GroupStyle>
            </DataGrid>
        </Grid>
    </TabItem>
</TabControl>

PS: I'm using VS2010 for this

PS:我为此使用 VS2010

回答by Mohit Shrivastava

XAML

XAML

<StackPanel>
    <Button Name="btnGetSongs" Content="Get Songs" Click="btnGetSongs_Click"></Button>
    <ProgressRing x:Name="progress1"/> <!-- Without MahApps -->
</StackPanel>

CS

CS

this.Dispatcher.Invoke((Action)(() => {
    progress1.IsActive = true;
    progress1.Visibility = Visibility.Visible;
    DataTable dtSong = new DataTable();
    //All Steps will go as stated
    view.GroupDescriptions.Add(new PropertyGroupDescription("Artist"));
    progress1.IsActive = false;
    progress1.Visibility = Visibility.Collapsed;
}));

MahApps

应用程序

XAML

XAML

<StackPanel>
    <Button Name="btnGetSongs" Content="Get Songs" Click="btnGetSongs_Click"></Button>
    <Controls:ProgressRing IsActive="False" Visibility="Collapsed" Name="progress1" />
</StackPanel>

CS

CS

this.Dispatcher.Invoke((Action)(() => {
    progress1.IsActive = true;
    progress1.Visibility = Visibility.Visible;
    DataTable dtSong = new DataTable();
    //All Steps will go as stated
    view.GroupDescriptions.Add(new PropertyGroupDescription("Artist"));
    progress1.IsActive = false;
    progress1.Visibility = Visibility.Collapsed;
}));