user3130331
user3130331

Reputation: 85

In a WPF Program, I want to change the Stroke Color on all the "Lines" on a "Canvas"

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

Answers (1)

Walt Ritscher
Walt Ritscher

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

Related Questions