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

本文关键字:数据 ProgressRing 加载 Mahapps WPF 显示 | 更新日期: 2023-09-27 18:11:11

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

背后的代码
    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

<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:我使用VS2010为这个

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

XAML

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

c

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

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

c

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;
}));