Set Text attribute for multiple text blocks

Is it possible to set an attribute Textfrom several TextBlockwithout calling each separately? The ability to iterate through them?

As in the following example:

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

For

<TextBlock x:Name="textblock_a" Text="Modified text"/>
<TextBlock x:Name="textblock_b" Text="Modified text"/>
+4
source share
2 answers

Probably the easiest way:

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

PS: I would not use the word without mentioning the attribute xaml, Text- property.

+3
source

WPF's way to do this is to use bindings.

As a state HERE (a simple example), you can bind Textyour value TextBlocksto the same property.

Do not forget INotifyPropertyChanged, therefore, when changing the line, everything is updated.

+3
source

All Articles