C# AvaloniaUI ProgressBar用法

AvaloniaUI ProgressBar 基本样式和用法

在AvaloniaUI中使用ProgressBar控件时,可以通过以下代码定义基本样式和绑定数据:

csharp 复制代码
<ProgressBar 
    Minimum="0" 
    Maximum="100" 
    Value="{Binding ProgressValue}" 
    Width="200" 
    Height="20"/>

对应的ViewModel需要包含进度值属性:

csharp 复制代码
private double _progressValue;
public double ProgressValue
{
    get => _progressValue;
    set => this.RaiseAndSetIfChanged(ref _progressValue, value);
}

自定义进度条样式

通过Avalonia的样式系统可以修改进度条外观:

xml 复制代码
<Style Selector="ProgressBar">
    <Setter Property="Background" Value="#E0E0E0"/>
    <Setter Property="Foreground" Value="#0078D7"/>
    <Setter Property="Height" Value="10"/>
    <Setter Property="CornerRadius" Value="5"/>
</Style>

<Style Selector="ProgressBar:disabled">
    <Setter Property="Opacity" Value="0.6"/>
</Style>

动画效果实现

为进度变化添加平滑动画:

xml 复制代码
<Style Selector="ProgressBar">
    <Setter Property="Template">
        <ControlTemplate>
            <Border Background="{TemplateBinding Background}"
                    CornerRadius="{TemplateBinding CornerRadius}">
                <Rectangle Name="PART_Track"
                          Fill="{TemplateBinding Foreground}"
                          CornerRadius="{TemplateBinding CornerRadius}"
                          Width="{TemplateBinding Value, Converter={StaticResource PercentageConverter}}"/>
            </Border>
        </ControlTemplate>
    </Setter>
    <Style.Animations>
        <Animation Duration="0:0:0.3" Easing="Linear">
            <KeyFrame Cue="0%">
                <Setter Property="Opacity" Value="0.5"/>
            </KeyFrame>
            <KeyFrame Cue="100%">
                <Setter Property="Opacity" Value="1"/>
            </KeyFrame>
        </Animation>
    </Style.Animations>
</Style>

不确定状态进度条

处理未确定进度的场景:

xml 复制代码
<ProgressBar IsIndeterminate="True" 
             Width="200"
             Height="8">
    <ProgressBar.Styles>
        <Style Selector="ProgressBar:indeterminate">
            <Setter Property="Foreground" Value="#FF5722"/>
        </Style>
    </ProgressBar.Styles>
</ProgressBar>

进度文本显示

在进度条上叠加文本显示百分比:

xml 复制代码
<Grid Width="200" Height="24">
    <ProgressBar Value="{Binding ProgressValue}"
                 Minimum="0" 
                 Maximum="100"/>
    <TextBlock Text="{Binding ProgressValue, StringFormat={}{0}%}"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"/>
</Grid>

垂直进度条实现

通过旋转实现垂直方向进度条:

xml 复制代码
<ProgressBar Width="20" 
             Height="100"
             Rotation="-90"
             HorizontalAlignment="Left"
             Value="{Binding ProgressValue}"/>

注意事项

  1. 确保Value属性始终在MinimumMaximum范围内,否则会引发异常
  2. 使用数据绑定时实现INotifyPropertyChanged接口确保UI更新
  3. 复杂样式建议放在资源字典中复用
  4. 不确定模式(IsIndeterminate)与确定模式互斥
  5. 高精度进度建议使用double类型而非int
相关推荐
IvanCodes1 小时前
Python 数据处理(十三):JSON、CSV 与数据序列化
开发语言·python
aramae3 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法
淡海水4 小时前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
asdzx675 小时前
如何使用 C# 提取 PPT 文本与图片
c#·ppt·提取文本
qq_2518364575 小时前
springboot vue3 开发实现 拼豆管理系统
java·开发语言·ai编程
郑州光合科技余经理6 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
知识分享小能手6 小时前
C++ 学习教程,从入门到精通,C++ 入门知识 — 完整知识点(1)
开发语言·c++·学习
+VX:Fegn08957 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
阿里嘎多学长7 小时前
2026-09-20 GitHub 热点项目精选
开发语言·程序员·github·代码托管
weixin_307779139 小时前
C++代码实现MATLAB中的ode23tb函数功能
开发语言·c++·算法·matlab