Reputation:
In my app (C# WPF) I have about 30 or 40 textBoxes in more grids and I want to change their foreground color in a loop. I use the code below and it works. But I want to use it for the whole project, not only for concrete grid
xaml code
<grid x:Name"stk">
.... some textBoxes ...
</grid>
*.cs code
foreach (TextBox item in this.stk.Children.OfType<TextBox>())
{
if (item.Name.StartsWith("txt"))
item.Foreground = Brushes.Orange;
}
So, when I have more grids, I have to put x:Name="..."
into each one and this implies more foreach loops.
Upvotes: 3
Views: 7923
Reputation:
So ... To solve my problem where I couldn't change foreground color of textBoxes when some textBox is disabled ... I used code below ...
<Application.Resources>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Orange"/>
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
Upvotes: 0
Reputation: 955
Much Simpler Way
Define a Style with TargetType set to Textbox and with no Key. This way this style will be applied to all textbox in the application without the need to bind the style or the foreground for each textbox.
<Application.Resources>
<SolidColorBrush Color="Red" x:Key="txtColor" />
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="{DynamicResource txtColor}" />
</Style>
</Application.Resources>
To change the Foreground Color.
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Application.Current.Resources.Contains("txtColor"))
{
Application.Current.Resources["txtColor"] = new SolidColorBrush(Colors.Blue);
}
}
Upvotes: 4
Reputation: 955
Bind all your Textbox's Foreground to a common Brush Resource. Define the brush resource common to Project and access it everywhere.
In App.XML declare the brush resource so that you can access it anywhere from your project. [Note : You can also define it resource Dictionary and refer it]
<Application.Resources>
<SolidColorBrush Color="Red" x:Key="txtColor" />
</Application.Resources>
In All your textbox bind the foreground to the "txtColor" brush resource.
<TextBox Foreground="{DynamicResource txtColor}" Text="TextBox" />
To change the Foreground color of all textbox's, then change the commonly defined resource's color. Below I changed the color in button click. Access th resource using the key and set the new brush which you want to set.
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Application.Current.Resources.Contains("txtColor"))
{
Application.Current.Resources["txtColor"] = new SolidColorBrush(Colors.Blue);
}
}
Upvotes: 1
Reputation: 414
Ignore my code and have a look at this answer
Find all controls in WPF Window by type
Upvotes: 0
Reputation: 5655
What about creating a "usercontrol" based on the standard textbox where you control the appearance of the foreground. This way you have a reusable control that you can use anywhere you want and have "full control" over it's appearance and behaviour. Take a look at this article, or this one for some examples that might help you go the right way ;)
Upvotes: -1