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

}

页面效果

相关推荐
“抚琴”的人1 天前
C# 取消机制(CancellationTokenSource/CancellationToken)
开发语言·c#·wpf·1024程序员节·取消机制
紫薯馍馍1 天前
Adobe Photoshop 2025(Ps2025)下载安装教程
ui·adobe·photoshop
兰亭妙微1 天前
2026年UX/UI五大趋势:AI、AR与包容性设计将重新定义用户体验
开发语言·ui·1024程序员节·界面设计·设计趋势
清风徐来Groot2 天前
WPF之Style
wpf
摘星编程2 天前
【参赛心得】HarmonyOS创新赛获奖秘籍:如何用Stage模型和声明式UI打造高分作品
ui·华为·harmonyos·鸿蒙开发·stage模型
rollingman2 天前
WPF GroupBox 淡入淡出
wpf
yy7634966682 天前
WPF 之 简单高效的Revit多语言支持方案
java·大数据·linux·服务器·wpf
张人玉2 天前
WPF 触发器详解:定义、种类与示例
c#·wpf·1024程序员节·布局控件
rollingman2 天前
WPF ComboBox 样式
wpf·combobox·textbox
知识分享小能手3 天前
uni-app 入门学习教程,从入门到精通,uni-app中uCharts组件学习((8)
vue.js·学习·ui·微信小程序·小程序·uni-app·echarts