4

Is it possible to set the Text attribute of multiple TextBlocks without calling each one separately? The possibility to iterate through them?

Like in the following example:

<TextBlock x:Name="textblock_a" Text="Original text"/>
<TextBlock x:Name="textblock_b" Text="Original text"/>

To

<TextBlock x:Name="textblock_a" Text="Modified text"/>
<TextBlock x:Name="textblock_b" Text="Modified text"/>
Sinatr
  • 20,892
  • 15
  • 90
  • 319
Michel Sahli
  • 1,059
  • 10
  • 14

2 Answers2

3

Probably the easiest way:

foreach(var item in new[] {textblock_a, textblock_b})
    item.Text = "Modified text";

P.S.: I wouldn't use word attribute without mentioning xaml, Text is a property.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
3

The WPF way of doing this is by using Binding.

As state HERE(easy example) you can bind the Text value of your TextBlocks to the same property.

Do not forget the INotifyPropertyChanged so everything updates when the string changes.

tweellt
  • 2,171
  • 20
  • 28