Set the foreground color for all text boxes in a grid in one place in XAML (Silverlight, WP)
Let's say we have a construction like this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" />
<TextBlock Grid.Row="0" Grid.Column="1" />
<TextBlock Grid.Row="1" Grid.Column="0" />
<TextBlock Grid.Row="1" Grid.Column="1" />
<TextBlock Grid.Row="2" Grid.Column="0" />
<TextBlock Grid.Row="2" Grid.Column="1" />
</Grid>
How can I set the foreground color for all text boxes in a grid as one parameter?
Something similar to
<Grid Color="Red">
...
</Grid>
+3
source to share
2 answers
You can add resources to the grid that make all of the textBlock's front borders red.
<Grid>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" >Good show</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" >Now the Foreground is red</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" />
<TextBlock Grid.Row="1" Grid.Column="1" />
<TextBlock Grid.Row="2" Grid.Column="0" />
<TextBlock Grid.Row="2" Grid.Column="1" />
</Grid>
+5
source to share
As an alternative to @ Onosa's answer - Foreground
is one of those dependency properties that inherit from the visual tree. This way you can also wrap your Grid in any subclass Control
, for example ContentControl
:
<ContentControl Foreground="Red">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" />
<TextBlock Grid.Row="0" Grid.Column="1" />
<TextBlock Grid.Row="1" Grid.Column="0" />
<TextBlock Grid.Row="1" Grid.Column="1" />
<TextBlock Grid.Row="2" Grid.Column="0" />
<TextBlock Grid.Row="2" Grid.Column="1" />
</Grid>
</ContentControl>
+5
source to share