Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.2 kB
1
Indexable
Never
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfApp
{
    public partial class MainWindow : Window
    {
        private ViewModel viewModel;

        public MainWindow()
        {
            InitializeComponent();

            // Set the DataContext to our ViewModel instance
            viewModel = new ViewModel();
            this.DataContext = viewModel;

            // Set up the animation
            var animation = new System.Windows.Media.Animation.DoubleAnimation
            {
                From = 0,
                To = 100,
                Duration = new Duration(TimeSpan.FromSeconds(5)),
                RepeatBehavior = System.Windows.Media.Animation.RepeatBehavior.Forever
            };
            progressBar.BeginAnimation(System.Windows.Controls.ProgressBar.ValueProperty, animation);

            // Attach the event handler to update the positions
            progressBar.ValueChanged += ProgressBar_ValueChanged;
        }

        private void ProgressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            // Get the current progress value
            double progress = e.NewValue;

            // Calculate the angle for rotation
            double angle = progress * 360 / 100;

            // Update the ViewModel properties
            viewModel.Left1 = circle.ActualWidth / 2 + (circle.ActualWidth / 2) * Math.Cos(angle * Math.PI / 180);
            viewModel.Top1 = circle.ActualHeight / 2 + (circle.ActualHeight / 2) * Math.Sin(angle * Math.PI / 180);

            // Repeat this process for the other rectangles (Left2, Top2, Left3, Top3)
            viewModel.Left2 = /* Calculate Left2 position */;
            viewModel.Top2 = /* Calculate Top2 position */;
            viewModel.Left3 = /* Calculate Left3 position */;
            viewModel.Top3 = /* Calculate Top3 position */;
        }
    }

    public class ViewModel : INotifyPropertyChanged
    {
        // Implement INotifyPropertyChanged to update the bindings
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        // Properties for rectangle positions
        private double left1;
        public double Left1
        {
            get { return left1; }
            set
            {
                left1 = value;
                OnPropertyChanged();
            }
        }

        private double top1;
        public double Top1
        {
            get { return top1; }
            set
            {
                top1 = value;
                OnPropertyChanged();
            }
        }

        private double left2;
        public double Left2
        {
            get { return left2; }
            set
            {
                left2 = value;
                OnPropertyChanged();
            }
        }

        private double top2;
        public double Top2
        {
            get { return top2; }
            set
            {
                top2 = value;
                OnPropertyChanged();
            }
        }

        private double left3;
        public double Left3
        {
            get { return left3; }
            set
            {
                left3 = value;
                OnPropertyChanged();
            }
        }

        private double top3;
        public double Top3
        {
            get { return top3; }
            set
            {
                top3 = value;
                OnPropertyChanged();
            }
        }

        // Constructor to set initial positions if required
        public ViewModel()
        {
            // Set the initial positions of the rectangles if needed
            // For example:
            // Left1 = 100;
            // Top1 = 100;
            // Left2 = 150;
            // Top2 = 50;
            // Left3 = 50;
            // Top3 = 50;
        }
    }
}


<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Benz Icon Animation" Width="500" Height="500">
    <Grid>
        <Ellipse x:Name="circle" Width="200" Height="200" Stroke="Black" StrokeThickness="2">
            <Rectangle x:Name="rectangle1" Width="50" Height="50" Fill="Blue"
                       Canvas.Left="{Binding Path=Left1}" Canvas.Top="{Binding Path=Top1}" />
            <Rectangle x:Name="rectangle2" Width="50" Height="50" Fill="Red"
                       Canvas.Left="{Binding Path=Left2}" Canvas.Top="{Binding Path=Top2}" />
            <Rectangle x:Name="rectangle3" Width="50" Height="50" Fill="Green"
                       Canvas.Left="{Binding Path=Left3}" Canvas.Top="{Binding Path=Top3}" />
        </Ellipse>
        <ProgressBar x:Name="progressBar" Width="200" Height="200" VerticalAlignment="Top" HorizontalAlignment="Center"
                     Minimum="0" Maximum="100" Value="0"/>
    </Grid>
</Window>