【问题标题】:How to automatically put in the tag the name of the UIElement如何自动在标签中放入 UIElement 的名称
【发布时间】:2021-05-07 21:05:24
【问题描述】:

我知道这可能看起来很奇怪,但我需要自动在标签中放入图形元素的名称 例如

var tbkMain = new TextBlock(){Foreground = ...,..., Tag = ??? } <----here I wish automatically Tag= "TbkMain"

var grdSmall = new TextBlock(){Foreground = ...,..., Tag = ??? } <----here I wish automatically Tag= "grdSmall"

还有一些事后做的事情,比如

tbkMain.Tag = ???;

或者(甚至更好):

foreach(var element in grdMain.Children)
      element.Tag = ???

谢谢

我不得不承认我什至不知道这是否可能或从哪里开始。 谢谢

【问题讨论】:

    标签: c# tags uielement


    【解决方案1】:

    从`here你可以看到nameof就是你所需要的。

    因此例如:

    tbkMain.Tag = nameof(tbkMain);
    

    但这不是很有用,因为编写 tbkMain.Tag = "tbkMain"

    至于我书中的其他机会,如果你这样做是不可能的:

    foreach (UIElement item in grdAll.Children)
                item.Tag = nameOf(item);<----error
                    
    

    这是不可能的,因为您必须定义类型。 即使你这样做:

    foreach (UIElement item in grdAll.Children)
                {
                    if (item is TextBlock) (item as TextBlock).Tag = nameof(item);
                    if (item is StackPanel) (item as StackPanel).Tag = nameof(item);
                    ...
                }
    

    结果将是项目而不是真实姓名。因此无用。 对不起... `

    【讨论】:

      猜你喜欢
      • 2012-09-30
      • 2019-12-20
      • 2021-04-21
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 2014-07-12
      • 2023-01-05
      • 1970-01-01
      相关资源
      最近更新 更多