如何在WPF中更新DataGrid中的进度条?

cig3rfwq  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(378)

我是.Net的新手,我想知道当我在DataGrid上的特定行中单击“下载”按钮时,如何更新进度栏。由于解决此问题的资源有限,我不知道如何实现此目的。

<DataGrid AutoGenerateColumns="False" x:Name="servers" HorizontalAlignment="Center" Height="148" Margin="0,78,0,0" VerticalAlignment="Top" Width="772" PreviewMouseDoubleClick="clientPreview" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding Id}" Width="50"></DataGridTextColumn>
            <DataGridTemplateColumn Header="Progress" 
                                    Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ProgressBar 
                            Value="{Binding Progress}"
                            Minimum="0" Maximum="100" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Action" Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate >
                        <Button  Background="#FF00FF35" Click="beginDownload">Download</Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

生成的DataGrid如下所示:

BeginDownload方法如下所示:

private void beginDownload(object sender, RoutedEventArgs e)
    {
        Clients selected = servers.SelectedItem as Clients;
        if (selected != null)
        {
            if (selected.Id == 0)
            {
                MessageBox.Show("Selected Feild is Empty", Title = "Empty Feild Selected");
            }
            else
            {
                //Update Progess Bar and Other Methods

            }
        }
        else
        {
            MessageBox.Show("Selected Field is Empty", Title = "Empty Field Selected");
        }
    }

我只想知道如何更新特定行中的进度条。例如,如果我放一个for循环,将进度从0更新为100。如何将整数值绑定到进度条?
找到一个类似的问题,但没有答案-Wpf Datagrid change element in the same row on click

4uqofj5v

4uqofj5v1#

在WPF中,非常流行的MVVM方法使用命令模式。根据它..您需要有ViewViewModel..然后您将能够在网格中为Download按钮使用命令并向其传递参数。

public class ClientsViewModel : INotifyPropertyChanged
{  
   public DelegateCommand DownloadCommand {get; private set;}

   // ctor
   public ClientsVM() 
   {
        DownloadCommand  = new DelegateCommand(DownloadExecute));
   }

   // async method allow to not block UI thread 
   public async void DownloadExecute(object param)
   {
       var client = param as Clients;
       if (client==null) return;

       // TODO: call real code for download 
       for (int i=0; i<100; i++)
       {
          await Task.Delay(100); // slow changes of progress
          client.Progress = i;   // if setter of Progress raised NotifyPropertyChanged.. it will update ProgressBar in your DataGrid
       }      
   }
}

要使用DownloadCommand,只需将其绑定到Button的属性

<DataGrid AutoGenerateColumns="False" x:Name="servers" HorizontalAlignment="Center" Height="148" Margin="0,78,0,0" VerticalAlignment="Top" Width="772" PreviewMouseDoubleClick="clientPreview">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding Id}" Width="50"></DataGridTextColumn>
            <DataGridTemplateColumn Header="Progress" 
                                    Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ProgressBar 
                            Value="{Binding Progress}"
                            Minimum="0" Maximum="100" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Action" Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate >
                        <Button  Background="#FF00FF35" Command="{Binding DownloadCommand, Mode=OneWay}" CommandParameter="{Binding}">Download</Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
55ooxyrt

55ooxyrt2#

Page.xaml.cs中我的解决方案:

Visual vis;
private void beginDownload(object sender, RoutedEventArgs e)
{
    Clients selected = servers.SelectedItem as Clients;
    if (selected != null)
    {
        if (selected.Id == 0)
        {
            MessageBox.Show("Selected Feild is Empty", Title = "Empty Feild Selected");
        }
        else
        {
            for (vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
                    if (vis is DataGridRow) //Find current DataGridRow
                    {
                        ProgressChanged();
                        //Other methods
                        break;
                    }
        }
    }
    else
    {
        MessageBox.Show("Selected Field is Empty", Title = "Empty Field Selected");
    }
}
private void ProgressChanged()
    {
        // Update the progressbar percentage 
        for (int i = 0; i <= 100; i++)
        {
            ((Clients)(((DataGridRow)vis).Item)).Progress = i;
        }
    }

我的客户类:

public class Clients : INotifyPropertyChanged
{
    private int mProgress;
    public Clients()
    {
        Id = 0;
    }

    public int Id { get; set; }

    public int Progress
    {
        get { return mProgress; }
        set
        {
            mProgress = value;
            OnPropertyChanged("Progress");
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

我还有一个问题,我的进度条速度太快,我如何减慢进度条速度?

相关问题