0

我想知道在向画布添加子项之前是否有办法刷新画布?我有这个代码,它给我画了一个带有浅蓝色笔划的黑色椭圆。现在我想在程序启动时更改 StrokeThickness(我有一个滑块来定义 StrokeThickness)。问题是 StrokeThickness 发生了变化,但前提是我重绘了椭圆,但我希望在移动滑块时进行更改。有任何想法吗?谢谢!

            //this code is in canvas_MouseDown
            double smt = sliderThickness.Value;

            //krog
            elip = new Ellipse
            {
                Width = 100,
                Height = 100,
                Fill = Brushes.Black,
                Stroke = Brushes.LightBlue,
                StrokeThickness = smt,
            };

            Canvas.SetLeft(elip, mouseX - 50);
            Canvas.SetTop(elip, mouseY - 50);

            canvas1.Children.Add(elip);
4

2 回答 2

3

在滑块值 Changedevent 中,您必须输入以下代码

 private void sliderThickness_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        foreach (var item in canvas1.Children)
        {
            if (item is Ellipse)
            {
                var elip = item as Ellipse;
                elip.StrokeThickness = sliderThickness.Value;
            }
        }
    }
于 2013-01-07T17:46:37.280 回答
0

或者更短

// using System.Linq;

foreach (var ellipse in canvas1.Children.OfType<Ellipse>())
{
    ellipse.StrokeThickness = sliderThickness.Value;
}
于 2013-01-07T17:59:55.997 回答