Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
1.4 kB
1
Indexable
Never
using System.Windows;
using System.Windows.Media;

public class ViewModel
{
    public Point GetCircularProgressBarCenter(FrameworkElement rootElement)
    {
        // Find the circular progress bar within the visual tree
        CircularProgressBar circularProgressBar = FindCircularProgressBar(rootElement);

        if (circularProgressBar != null)
        {
            // Calculate the center point of the circular progress bar
            Point circularProgressBarCenter = new Point(
                circularProgressBar.ActualWidth / 2,
                circularProgressBar.ActualHeight / 2);

            return circularProgressBar.TransformToAncestor(rootElement).Transform(circularProgressBarCenter);
        }

        // Return a default point if circular progress bar is not found
        return new Point();
    }

    private CircularProgressBar FindCircularProgressBar(DependencyObject parent)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);
            if (child is CircularProgressBar circularProgressBar)
            {
                return circularProgressBar;
            }
            else
            {
                CircularProgressBar result = FindCircularProgressBar(child);
                if (result != null)
                    return result;
            }
        }

        return null;
    }
}