Reputation: 85
I have a bunch of lines on a canvas. I want to iterate through the Lines and turn their Stroke colors to black.
The line of code in the foreach loop won't compile.
foreach (FrameworkElement Framework_Element in My_Canvas.Children)
{
//the following line of code won't compile.
(Line)Framework_Element.Stroke = new SolidColorBrush(Colors.Black);
}
Upvotes: 1
Views: 11218
Reputation: 7037
You are missing a pair of parenthesis.
foreach (FrameworkElement Framework_Element in My_Canvas.Children)
{
// tries to find .Stroke on the FrameworkElement class
// (Line)Framework_Element.Stroke
// correct way
((Line)Framework_Element).Stroke = new SolidColorBrush(Colors.Black);
// or
var currentLine = (Line)Framework_Element;
currentLine.Stroke = new SolidColorBrush(Colors.Black);
}
Upvotes: 6