Find all controls in WPF Window by type

后端 未结 17 1297
春和景丽
春和景丽 2020-11-21 10:14

I\'m looking for a way to find all controls on Window by their type,

for example: find all TextBoxes, find all controls implementing specific i

17条回答
  •  Happy的楠姐
    2020-11-21 10:38

    I wanted to add a comment but I have less than 50 pts so I can only "Answer". Be aware that if you use the "VisualTreeHelper" method to retrieve XAML "TextBlock" objects then it will also grab XAML "Button" objects. If you re-initialize the "TextBlock" object by writing to the Textblock.Text parameter then you will no longer be able to change the Button text using the Button.Content parameter. The Button will permanently show the text written to it from the Textblock.Text write action (from when it was retrieved --

    foreach (TextBlock tb in FindVisualChildren(window))
    {
    // do something with tb here
       tb.Text = ""; //this will overwrite Button.Content and render the 
                     //Button.Content{set} permanently disabled.
    }
    

    To work around this, you can try using a XAML "TextBox" and add methods (or Events) to mimic a XAMAL Button. XAML "TextBox" is not gathered by a search for "TextBlock".

提交回复
热议问题