将动画添加到我在代码 c# wpf 中创建的椭圆
Adding animation to an ellipse that I've created in code c# wpf
我在代码中创建了多个椭圆并添加了 MouseEnter 和 Leave 事件。我的问题是,当我用光标进入椭圆时,它会改变整个 window 的不透明度,而不仅仅是那个椭圆。
在这里,我创建了椭圆:
for (int i = 0; i < L2.Count; i++)
{
Ellipse myEllipse = new Ellipse();
myEllipse.Opacity = .5;
myEllipse.MouseEnter += MyEllipse_MouseEnter;
myEllipse.MouseLeave += MyEllipse_MouseLeave;
users_profiles.Children.Add(myEllipse);
}
和事件:
private void MyEllipse_MouseLeave(object sender, MouseEventArgs e)
{
DoubleAnimation open_an = new DoubleAnimation();
open_an.From = 1;
open_an.To = .5;
open_an.Duration = TimeSpan.FromSeconds(.3);
BeginAnimation(OpacityProperty, open_an); // this.BeginAnimation(...) has the same result.
}
it changes the opacity of the whole window
显然,因为您在 MainWindow 实例上调用 BeginAnimation
。
从发件人参数中获取椭圆:
private void MyEllipse_MouseLeave(object sender, MouseEventArgs e)
{
var ellipse = (Ellipse)sender;
var open_an = new DoubleAnimation
{
From = 1,
To = .5,
Duration = TimeSpan.FromSeconds(.3)
};
ellipse.BeginAnimation(UIElement.OpacityProperty, open_an);
}
我在代码中创建了多个椭圆并添加了 MouseEnter 和 Leave 事件。我的问题是,当我用光标进入椭圆时,它会改变整个 window 的不透明度,而不仅仅是那个椭圆。
在这里,我创建了椭圆:
for (int i = 0; i < L2.Count; i++)
{
Ellipse myEllipse = new Ellipse();
myEllipse.Opacity = .5;
myEllipse.MouseEnter += MyEllipse_MouseEnter;
myEllipse.MouseLeave += MyEllipse_MouseLeave;
users_profiles.Children.Add(myEllipse);
}
和事件:
private void MyEllipse_MouseLeave(object sender, MouseEventArgs e)
{
DoubleAnimation open_an = new DoubleAnimation();
open_an.From = 1;
open_an.To = .5;
open_an.Duration = TimeSpan.FromSeconds(.3);
BeginAnimation(OpacityProperty, open_an); // this.BeginAnimation(...) has the same result.
}
it changes the opacity of the whole window
显然,因为您在 MainWindow 实例上调用 BeginAnimation
。
从发件人参数中获取椭圆:
private void MyEllipse_MouseLeave(object sender, MouseEventArgs e)
{
var ellipse = (Ellipse)sender;
var open_an = new DoubleAnimation
{
From = 1,
To = .5,
Duration = TimeSpan.FromSeconds(.3)
};
ellipse.BeginAnimation(UIElement.OpacityProperty, open_an);
}