在 WPF 程序中,我想更改“画布”上所有“线条”的描边颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22415136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:13:50  来源:igfitidea点击:

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

c#wpfcanvas

提问by user3130331

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 循环中的代码行不会编译。

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

回答by Walt Ritscher

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);
  }