WPF circle progress bar

前端 未结 2 1480
抹茶落季
抹茶落季 2021-02-05 14:18

I want to replace the regular ProgressBar with circle one and after shot search here in the forum i found what i want.

CircularProgressBar.XAML



        
2条回答
  •  無奈伤痛
    2021-02-05 14:54

    This demonstrates how to animate the circle.

    When AnimateProgressCircle is true, the "busy" circle will be rotating, else it will be invisible.

    
    
        
            
            
                
                
                    
                
            
        
    
    

    Advantages

    • As it's wrapped in a , it will always be perfectly sized to the parent container.
    • Unlike other simplistic solutions, it does not consume resources when it is not in use because the animation is stopped.

    Testing

    Tested on:

    • WPF
    • .NET 4.5 + .NET 4.6.1
    • Win7 x64
    • Visual Studio 2015 + Update 2

    To test, set DataContext="{Binding RelativeSource={RelativeSource Self}}" in the tag, then use this code behind.

    You should see the circle pause for 2 seconds, then animate for 4 seconds, then stop.

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private bool _ifAnimateProgressCircle;
    
        public MainWindow()
        {
            InitializeComponent();
            Task.Run(
                async () =>
                {
                    // Animates circle for 4 seconds.
                    IfAnimateProgressCircle = false;
                    await Task.Delay(TimeSpan.FromMilliseconds(2000));
                    IfAnimateProgressCircle = true;
                    await Task.Delay(TimeSpan.FromMilliseconds(6000));
                    IfAnimateProgressCircle = false;
                });
        }
    
        public bool IfAnimateProgressCircle
        {
            get { return _ifAnimateProgressCircle; }
            set { _ifAnimateProgressCircle = value; OnPropertyChanged(); }
        }
    
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
    
        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
    

    Links that helped with this solution

    • WPF. How to stop data trigger animation through binding?
    • Animate UserControl in WPF?
    • Binding objects defined in code-behind
    • Triggers collection members must be of type EventTrigger

提交回复
热议问题