private void Ball_MonitoringChanged(object sender, MonitoringArgs e) { if(e.valor) //Important to avoid recursivity { if(monitoredBall != null) { if(monitoredBall != (Ball)sender) monitoredBall.Monitoring = false; } monitoredBall = (Ball)sender; } }
private void btnCreateBall_Click(object sender, System.Windows.RoutedEventArgs e) { Ball myBall = new Ball(LayoutRoot.Height, ref txtBallTop, ref txtBallLeft); myBall.MonitoringChanged += new MonitoringHaldler(Ball_MonitoringChanged); Canvas.SetLeft(myBall, (new Random(DateTime.Now.Millisecond).NextDouble()) * (LayoutRoot.Width - myBall.Width)); Canvas.SetTop(myBall, 0); LayoutRoot.Children.Add(myBall); balls.Add(myBall.GetHashCode(), myBall); updateLabels(); }