3

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.

Pavel Gatilov
  • 7,579
  • 1
  • 27
  • 42

5 Answers5

4

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);
    }
}
Sivasubramanian
  • 935
  • 7
  • 22
  • But ... this work nearly perfectly but only if textBoxes are enables .. if they are disable it doesnt work ... why? Normaly I can set foreground color ... but not in this way ... I dont understand –  Jun 18 '14 at 11:02
  • It works for me. Copy paste the textbox XAML for which the foreground not applied when it is in disabled state. Also make sure you are not assigning Foreground color manually from codebehind – Sivasubramanian Jun 18 '14 at 11:16
  • in textBox is just set –  Jun 18 '14 at 11:30
  • TextBox.Text = ??? Give some text in textBox. If still foreground not changed then Create a simple sample with only two textbox with enabled state to false and in button click change the foreground using the above way. If issue not reproduced in sample level then u can make sure you change the foreground somewher in ur APP. For me it works. – Sivasubramanian Jun 18 '14 at 11:44
  • This is my code: I use code above .. I would like to send you photo but I have just 8 rep. For first one it works for second one it doesnt ... I have nothing else in my app –  Jun 18 '14 at 11:55
  • Ok I got it. You are using .NET 3.5 Framework. In 4.0 and above it works properly but in Net 3.5 like you said, disabled Textbox foreground is not changed. – Sivasubramanian Jun 18 '14 at 12:05
  • I use .NET Framework 4 ... I found it in project->properties->Application_tab->Target framework: .NET Framework 4 and in Visual Studio 2012 where I use .Net Framework 4.5, it is same –  Jun 18 '14 at 12:26
1

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);
    }
}
Sivasubramanian
  • 935
  • 7
  • 22
  • And can you tell me how to do it ... or show me some example? thanks –  Jun 18 '14 at 09:07
  • :), You are kidding. Below is my another solution which i hope will be much simpler for you. You dont need to bind the color for your 30 or 40 textboxes. – Sivasubramanian Jun 18 '14 at 09:31
0

Ignore my code and have a look at this answer

Find all controls in WPF Window by type

Community
  • 1
  • 1
Inept Adept
  • 414
  • 4
  • 11
0

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>
-1

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 ;)

Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59