WPF数据加载时添加进度条

XML 复制代码
<Window x:Class="YourNamespace.YourWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="YourWindow" Height="350" Width="525">
    <Grid>
        <!-- 其他UI元素 -->

        <!-- 透明的覆盖层,显示在页面上方,包含进度条 -->
        <Grid Visibility="{Binding IsLoading, Converter={StaticResource BoolToVisibilityConverter}}" 
              Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ZIndex="1">
            <Border Background="Black" Opacity="0.5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <!-- 进度条居中显示 -->
                <ProgressBar x:Name="LoadingProgressBar"
                             IsIndeterminate="True"
                             Height="100" 
                             Width="200" 
                             HorizontalAlignment="Center"
                             VerticalAlignment="Center" 
                             Margin="0, 50, 0, 0" />
            </Border>
        </Grid>

        <!-- 后面的UI元素 -->
        <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" ZIndex="0">
            <TextBlock Text="Your content goes here..." />
            <!-- 更多元素 -->
        </StackPanel>
        
    </Grid>
</Window>

viewModel代码

cs 复制代码
 // 控制进度条的可见性
 [ObservableProperty]
 private Visibility isLoading = Visibility.Collapsed; // 默认不显示进度条

private void ChangeLoadingStatus(bool isLoading) {

    if (isLoading)
    {
        IsLoading = Visibility.Visible;
    }
    else
    {
        IsLoading = Visibility.Collapsed;
    }

}

[RelayCommand]
private  async void SelectInfo()
{
  string sql ="....."
  ChangeLoadingStatus(true);
  
  var result = await Task.Run(() => dal.db.SqlQueryable<ToolStr50>(sql).ToList());

  ChangeLoadingStatus(false); 

}

页面效果

相关推荐
袅沫20 小时前
Element-UI 番外表格组件
javascript·vue.js·ui
百锦再1 天前
[特殊字符] HBuilder uni-app UI 组件库全方位对比
android·java·开发语言·ui·rust·uni-app·go
Macbethad2 天前
使用WPF编写一个多维度伺服系统的程序
大数据·hadoop·wpf
lingxiao168882 天前
WPF Prism框架应用
c#·wpf·prism
Macbethad2 天前
使用WPF编写一个Ethercat主站的程序
wpf
难搞靓仔2 天前
WPF 弹出窗体Popup
wpf·popup
Macbethad3 天前
使用WPF编写一个MODBUSTCP通信的程序
wpf
unicrom_深圳市由你创科技3 天前
Avalonia.WPF 跨平台图表的使用
wpf
lqj_本人3 天前
鸿蒙与Qt的双线程模型:主线程与UI线程的博弈
qt·ui·harmonyos
南华3 天前
Visual Studio 2022 加载解决方案缓慢
ui