1.编写圆形进度环组件CircularProgressBar.xam

<UserControl x:Class="Wpf.Industrial.Controls.CircularProgressBar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Wpf.Industrial.Controls"
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="150">
    <Grid Name="layout" Height="{Binding RelativeSource={RelativeSource Self},Path=Width}">
        <Ellipse Width="{Binding ElementName=layout,Path=ActualWidth}"
                 Height="{Binding RelativeSource={RelativeSource Self},Path=Width}"
                 StrokeThickness="6"
                 Stroke="{Binding BackColor,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor}}" 
                 Name="backEllipse">
            <Ellipse.Effect>
                <DropShadowEffect ShadowDepth="0" Direction="0" Color="White" BlurRadius="5" />
            </Ellipse.Effect>
        </Ellipse>

        <Path Name="path" StrokeStartLineCap="Round" StrokeEndLineCap="Round"
         Stroke="{Binding ForeColor,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor}}" 
         StrokeThickness="4"/>
        
        <Viewbox Margin="14">
            <TextBlock  VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White">
                  <Run FontSize="20" Text="{Binding Value,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor}}"/>
                  <Run Text="%"/>
            </TextBlock>
        </Viewbox>
    </Grid>
</UserControl>
View Code

 

2.后台代码实现进度值和颜色的逻辑交互CircularProgressBar.xam.cs

    /// <summary>
    /// CircularProgressBar.xaml 的交互逻辑
    /// </summary>
    public partial class CircularProgressBar : UserControl
    {
        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(CircularProgressBar), new PropertyMetadata(0.0, new PropertyChangedCallback(OnPropertyChanged)));

        public Brush BackColor
        {
            get { return (Brush)GetValue(BackColorProperty); }
            set { SetValue(BackColorProperty, value); }
        }
        public static readonly DependencyProperty BackColorProperty =
            DependencyProperty.Register("BackColor", typeof(Brush), typeof(CircularProgressBar), new PropertyMetadata(Brushes.LightGray));

        public Brush ForeColor
        {
            get { return (Brush)GetValue(ForeColorProperty); }
            set { SetValue(ForeColorProperty, value); }
        }
        public static readonly DependencyProperty ForeColorProperty =
            DependencyProperty.Register("ForeColor", typeof(Brush), typeof(CircularProgressBar), new PropertyMetadata(Brushes.Orange));



        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as CircularProgressBar).UpdateValue();
        }

        public CircularProgressBar()
        {
            InitializeComponent();

            this.SizeChanged += CircularProgressBar_SizeChanged;
        }

        private void CircularProgressBar_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.UpdateValue();
            this.SizeChanged += CircularProgressBar_SizeChanged;
        }

        private void UpdateValue()
        {
            this.layout.Width = Math.Min(this.RenderSize.Width, this.RenderSize.Height);
            this.layout.Height = Math.Min(this.RenderSize.Width, this.RenderSize.Height);
            double radius = Math.Min(this.RenderSize.Width, this.RenderSize.Height) / 2;
            if (radius <= 0) return;

            double newX = 0.0, newY = 0.0;
            newX = radius + (radius - 3) * Math.Cos((this.Value % 100.0 * 3.6 - 90) * Math.PI / 180);
            newY = radius + (radius - 3) * Math.Sin((this.Value % 100.0 * 3.6 - 90) * Math.PI / 180);

            string pathDataStr = "M{0} 3A{3} {3} 0 {4} 1 {1} {2}";
            pathDataStr = string.Format(pathDataStr,
                radius + 0.01,
                newX,
                newY,
                radius - 3,
                this.Value < 50 ? 0 : 1);
            var converter = TypeDescriptor.GetConverter(typeof(Geometry));
            this.path.Data = (Geometry)converter.ConvertFrom(pathDataStr);

        }
    }
View Code

 

3.其他页面使用该组件展示环形进度

                <UniformGrid Columns="3" Margin="0,40,0,0">
                    <StackPanel>
                        <TextBlock Text="监控值" HorizontalAlignment="Center" Foreground="#99FFFFFF"/>
                        <control:CircularProgressBar Value="10"  Width="60" Height="90" BackColor="#EEE" ForeColor="#20c9b4"/>
                    </StackPanel>
                    <StackPanel>
                        <TextBlock Text="监控值" HorizontalAlignment="Center" Foreground="#99FFFFFF"/>
                        <control:CircularProgressBar Value="63" Width="60" Height="90" BackColor="#EEE" ForeColor="Orange"/>
                    </StackPanel>
                    <StackPanel>
                        <TextBlock Text="监控值" HorizontalAlignment="Center"  Foreground="#99FFFFFF"/>
                        <control:CircularProgressBar Value="38" Width="60" Height="90" BackColor="#EEE" ForeColor="#38baec"/>
                    </StackPanel>
                </UniformGrid>
View Code

 

4.使用后显示的效果图

 

posted on 2025-01-14 15:44  江渔湖  阅读(57)  评论(0)    收藏  举报