Find WPF control by Name

后端 未结 2 458
逝去的感伤
逝去的感伤 2020-11-28 15:45

I\'m after some help finding the best way to refer to controls that have been programmtically built in C#

If I pre include a label in XAML and name it marketIn

相关标签:
2条回答
  • 2020-11-28 16:08

    There's at least two ways to do that:

    • Use the FindName method of the parent container to find the control (but it'll internally involve looping, like the visualtreehelper)

    • Create a dictionary to store a reference for each control you create

      var controls = new Dictionary<string, FrameworkElement>();
      controls.Add("_marketInfo5", lbl);
      

      Then you can do:

      controls["_marketInfo5"].Tag = timeNow;
      
    0 讨论(0)
  • 2020-11-28 16:10

    You can use XamlQuery for finding your controls at run-time.XamlQuery In CodePlex

    XamlQuery.Search(RegisterGrid, "Label[Name=_marketInfo5]").SetValue(Control.TagProperty, timeNow);
    
    0 讨论(0)
提交回复
热议问题