我想知道在添加子项之前是否有刷新画布的方法? 我有这个代码,它画了一个浅蓝色笔画的黑色椭圆。现在我想在程序启动时更改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);
答案 0 :(得分: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;
}
}
}
答案 1 :(得分:0)
或更短
// using System.Linq;
foreach (var ellipse in canvas1.Children.OfType<Ellipse>())
{
ellipse.StrokeThickness = sliderThickness.Value;
}