【问题标题】:How to get the content of a Label within a Grid如何在网格中获取标签的内容
【发布时间】:2013-04-28 15:07:50
【问题描述】:

我的 XAML 中有一个标签 (labelRecipeName),它是网格(称为 gridRecipe)的一部分,我想以编程方式获取该网格的上下文(字符串)。

我知道它总是我网格中的第一个元素,所以我在玩弄类似的东西:

gridRecipe.Children[0]

但我似乎无法让它工作...... 还是有更好的办法?

我希望找到类似的东西

string receipeName = gridRecipe.labelRecipeName

但显然我没那么幸运 :) 任何帮助将非常感激。 谢谢,

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    这样您就可以在Grid 中获得Label。然后就可以得到label的内容了:

    var label = gridRecipe.Children.OfType<Label>()
                                .First(i => i.Name == "labelRecipeName");
    
    var result = label.Content.ToString();
    

    【讨论】:

    • 这个有问题。它不仅会返回内容,还会返回“System.Controlls.Label: BLAH BLAH BLAH”,当我们只需要 Blah blah blah 部分时......
    【解决方案2】:

    如果您的标签可以在代码隐藏中通过名称访问,那么您可以这样做

    string receipeName = labelRecipeName.Content.ToString();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2021-06-02
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多