在我的应用程序中添加几个具有不同属性的用户控件

本文关键字:属性 控件 用户 几个 应用程序 我的 添加 | 更新日期: 2023-09-27 18:05:26

我找到了CircularProgressBar的这个实现。这是.cs文件:

public partial class CircularProgressBar : UserControl
{
    public CircularProgressBar()
    {
        InitializeComponent();
        Angle = (Percentage * 360) / 100;
        RenderArc();
    }
    public int Radius
    {
        get { return (int)GetValue(RadiusProperty); }
        set { SetValue(RadiusProperty, value); }
    }
    public Brush SegmentColor
    {
        get { return (Brush)GetValue(SegmentColorProperty); }
        set { SetValue(SegmentColorProperty, value); }
    }
    public int StrokeThickness
    {
        get { return (int)GetValue(StrokeThicknessProperty); }
        set { SetValue(StrokeThicknessProperty, value); }
    }
    public double Percentage
    {
        get { return (double)GetValue(PercentageProperty); }
        set { SetValue(PercentageProperty, value); }
    }
    public double Angle
    {
        get { return (double)GetValue(AngleProperty); }
        set { SetValue(AngleProperty, value); }
    }
    // Using a DependencyProperty as the backing store for Percentage.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty PercentageProperty =
        DependencyProperty.Register("Percentage", typeof(double), typeof(CircularProgressBar), new PropertyMetadata(65d, new PropertyChangedCallback(OnPercentageChanged)));
    // Using a DependencyProperty as the backing store for StrokeThickness.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty StrokeThicknessProperty =
        DependencyProperty.Register("StrokeThickness", typeof(int), typeof(CircularProgressBar), new PropertyMetadata(1));
    // Using a DependencyProperty as the backing store for SegmentColor.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SegmentColorProperty =
        DependencyProperty.Register("SegmentColor", typeof(Brush), typeof(CircularProgressBar), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
    // Using a DependencyProperty as the backing store for Radius.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty RadiusProperty =
        DependencyProperty.Register("Radius", typeof(int), typeof(CircularProgressBar), new PropertyMetadata(105, new PropertyChangedCallback(OnPropertyChanged)));
    // Using a DependencyProperty as the backing store for Angle.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AngleProperty =
        DependencyProperty.Register("Angle", typeof(double), typeof(CircularProgressBar), new PropertyMetadata(120d, new PropertyChangedCallback(OnPropertyChanged)));
    private static void OnPercentageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        CircularProgressBar circle = sender as CircularProgressBar;
        circle.Angle = (circle.Percentage * 270) / 100;
    }
    private static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        CircularProgressBar circle = sender as CircularProgressBar;
        circle.RenderArc();
    }
    public void RenderArc()
    {
        Point startPoint = new Point(Radius, 0);
        Point endPoint = ComputeCartesianCoordinate(Angle, Radius);
        endPoint.X += Radius;
        endPoint.Y += Radius;
        pathRoot.Width = Radius * 2 + StrokeThickness;
        pathRoot.Height = Radius * 2 + StrokeThickness;
        pathRoot.Margin = new Thickness(StrokeThickness, StrokeThickness, 0, 0);
        bool largeArc = Angle > 180.0;
        Size outerArcSize = new Size(Radius, Radius);
        pathFigure.StartPoint = startPoint;
        if (startPoint.X == Math.Round(endPoint.X) && startPoint.Y == Math.Round(endPoint.Y))
            endPoint.X -= 0.01;
        arcSegment.Point = endPoint;
        arcSegment.Size = outerArcSize;
        arcSegment.IsLargeArc = largeArc;
    }
    private Point ComputeCartesianCoordinate(double angle, double radius)
    {
        // convert to radians
        double angleRad = (Math.PI / 180.0) * (angle - 90);
        double x = radius * Math.Cos(angleRad);
        double y = radius * Math.Sin(angleRad);
        return new Point(x, y);
    }
}

因为我不想要完整的Circular,但只有一半,我已经改变了这个:

private static void OnPercentageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
    CircularProgressBar circle = sender as CircularProgressBar;
    circle.Angle = (circle.Percentage * 360) / 100;
}

360的值替换为180。现在我想在我的应用程序中添加相同的控件,但这次我希望这个Circular是在完整的Circle所以我的问题是我怎么能做到这一点呢?

在我的应用程序中添加几个具有不同属性的用户控件

你可以创建另一个依赖属性,它将作为圆形进度条的模式

    public enum Modes
    {
        Full = 360,
        Half = 180
    }
    public Modes CircularMode
    {
        get { return (Modes)GetValue(CircularModeProperty); }
        set { SetValue(CircularModeProperty, value); }
    }
    public static readonly DependencyProperty CircularModeProperty =
        DependencyProperty.Register("CircularMode", typeof(Modes), typeof(CircularProgressBar), new PropertyMetadata(Modes.Full));

你可以在你的计算中使用这个值:

circle.Angle = (circle.Percentage * (int)circle.CircularMode) / 100;

用法:

<xyz:CircularProgressBar CircularMode="Half" ... 

<xyz:CircularProgressBar CircularMode="Full" ... 

注意:'xyz'只是一个示例名称空间定义,在您的应用程序中可能会有所不同。