改变多边形颜色

本文关键字:颜色 多边形 改变 | 更新日期: 2023-09-27 18:02:36

我从c#代码中添加MouseEnter事件处理程序,如下所示:

var polygon = new Polygon { StrokeThickness = 1, Fill = Brushes.Blue };
polygon.MouseEnter += new MouseEventHandler(p_MouseLeftButtonDown2);
...
void p_MouseLeftButtonDown2(object sender, MouseEventArgs e)
{
   MessageBox.Show("Entered");
}

如何改变p_MouseLeftButtonDown2中多边形的颜色?

改变多边形颜色

有很多方法可以解决这个任务。我认为你在WPF中真的很新,也许在c#中也是,所以我想以下解决方案适合你:

void p_MouseLeftButtonDown2(object sender, MouseEventArgs e)
{
    Polygon poly = sender as Polygon;
    if (poly != null)
        poly.Fill = Brushes.Red;
}

Storyboard

将其放入p_MouseLeftButtonDown2方法:

((Polygon)sender).Fill = Brushes.Black; //whatever color you need

如果我是你,我会考虑用MVVM方法替换这整个代码隐藏的可能性。