0

I want to know if it's possible to let my code scan every name of every textblock in my XAML file and put it into an array.

for example:

XAML:

<TextBlock x:Name="textBlock1" />

C#:

string[] textblocknames = new string[] { "textBlock1", "2", ... };

I know how to scan for controls by type, which has been explained here: Find all controls in WPF Window by type. But I don't know how to find the names and put it in an array.

I want to use this to put borders around it, and it would look similiar to this:

        for (int i = 0; i < textblocknames.Length; i++)
        {
            Border brd = new Border();
            brd.Name = string.Format("border_{0}", i);
            brd.BorderThickness = new Thickness(0, 0, 1, 1);
            brd.BorderBrush = Brushes.Black;
            brd.Child = textblocknames[i]
        }

Can someone help me with this?

Thanks in advance!

Community
  • 1
  • 1
user5730942
  • 65
  • 2
  • 10
  • There are so many textblocks in my XAML file that I don't want to add these borders manually, and it's handy to know, in my opinion and I`m also curious – user5730942 Jan 16 '16 at 12:31
  • You want get all textblocks and change borders, or you want just get textblocks names? – Nejc Galof Jan 16 '16 at 12:32

1 Answers1

0

You can search all texblocks in this way: Solution. You can get names for every textblock in foreach loop like: tb.text, and put them to array. You can add border in this way:

  for (int i = 0; i < textblocknames.Length; i++)
        {
            Border brd = new Border();
            brd.Name = string.Format("border_{0}", i);
            brd.BorderThickness = new Thickness(0, 0, 1, 1);
            brd.BorderBrush = Brushes.Black;
            object item = GridName.FindName(textblocknames[i]);
            brd.Child = item;
        }
Community
  • 1
  • 1
Nejc Galof
  • 2,538
  • 3
  • 31
  • 70