【问题标题】:C# WPF set Tag=name for all controlsC# WPF 为所有控件设置 Tag=name
【发布时间】:2015-11-13 14:22:37
【问题描述】:

我有一个项目,其中包含各种控件,有些有名称,有些没有。 我想循环所有控件并在存在时自动设置 Tag=Name。 我见过像这样的各种解决方案:

WPF: How do I loop through the all controls in a window?

这可行,但我无法设置:

foreach (Visual ctrl in MainGrid.GetChildren())
{
    ctrl.Tag = ctrl.Name;<------
}

对我来说,该标签用于在按下不同按钮时识别事件 es。 谢谢 帕特里克

【问题讨论】:

  • 不清楚。什么有效,什么无效?

标签: c# wpf tags


【解决方案1】:

Tag 属性仅存在于FrameworkElements

所以你需要做一个演员:

foreach (Visual ctrl in MainGrid.GetChildren())
{
    FrameworkElement fxElt = ctrl as FrameworkElement;
    if( fxElt != null)
        fxElt.Tag = fxElt.Name;
}

问候

【讨论】:

  • 谢谢它的工作。您只是忘记更改最后一行。这不是 fxElt.Tag = ctrl.Name;但是 fxElt.Tag = fxElt.Name;
猜你喜欢
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
  • 2019-07-17
  • 2011-03-23
  • 1970-01-01
  • 2010-11-24
  • 2020-09-28
  • 1970-01-01
相关资源
最近更新 更多