首页 > 解决方案 > 在执行 ComboBox 任务时显示进度条

问题描述

在 ComboBox 中选择项目时,会复制一些与所选名称匹配的文件。这需要一些时间,并且在此期间屏幕只是冻结(列表下拉),我希望下拉的组合框列表上升并在复制文件时显示进度条。我尝试添加一个进度条,但它没有显示出来。

$ComboBox.Add_SelectionChanged(
{
    $progressBar.Visibility = "Visible"     #Show an Indeterminate Progress Bar at the beginning
    
    if($ComboBox.SelectedItem)
    {
        $selectedName = $ComboBox.SelectedItem
        Copy-Item -Path $storePath\* -Destination $tempPath -Filter $selectedName*
        $fileList = (Get-ChildItem -Path $tempPath).Name | Select-String $selectedName

        $ListBox.Items.Clear()
        foreach($file in $fileList)
        {
            $ListBox.Items.Add($fileList)
        }
    }
    
    $progressBar.Visibility = "Hidden"      #Hide the Progress Bar once done
})

标签: wpfpowershell

解决方案


作为一种解决方法,我使用“DropDownClosed”触发复制,同时使用“SelectionChanged”触发进度条。现在的问题是进度条卡住了。现在我需要另一种方法在单独的线程中运行 ProgressBar。

$ComboBox.Add_SelectionChanged(
{
    $progressBar.Visibility = "Visible"     #Show an Indeterminate Progress Bar at the beginning
})

$ComboBox.Add_DropDownClosed(
{
    if($ComboBox.SelectedItem)
    {
        $selectedName = $ComboBox.SelectedItem
        
        if(!((Get-ChildItem -Path $tempPath).Name | Select-String $selectedName))
        {
            Copy-Item -Path $storePath\* -Destination $tempPath -Filter $selectedName*
        }
        
        $fileList = (Get-ChildItem -Path $tempPath).Name | Select-String $selectedName

        $ListBox.Items.Clear()
        foreach($file in $fileList)
        {
            $ListBox.Items.Add($fileList)
        }
    }
    
    $progressBar.Visibility = "Hidden"      #Hide the Progress Bar once done
})

推荐阅读