在WPF程序中,我想更改"画布"上所有"线条"上的笔触颜色

use*_*331 1 c# wpf canvas

我在画布上有一堆线条.我想迭代线条并将它们的笔触颜色变为黑色.

foreach循环中的代码行将无法编译.

foreach (FrameworkElement Framework_Element in My_Canvas.Children)
{
    //the following line of code won't compile.
    (Line)Framework_Element.Stroke = new SolidColorBrush(Colors.Black);
}
Run Code Online (Sandbox Code Playgroud)

Wal*_*her 5

你缺少一对括号.

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);
  }
Run Code Online (Sandbox Code Playgroud)