wpf 如何更改应用程序中所有 TextBox 的前景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24280994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:56:55  来源:igfitidea点击:

How to change all TextBox's Foreground Color in an application

wpf

提问by Sivasubramanian

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

在我的应用程序(C# WPF)中,我在更多网格中有大约 30 或 40 个文本框,我想在循环中更改它们的前景色。我使用下面的代码并且它有效。但我想将它用于整个项目,而不仅仅是用于混凝土网格

xaml code

xaml 代码

<grid x:Name"stk">
    .... some textBoxes ...
</grid>

*.cs code

*.cs 代码

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.

所以,当我有更多的网格时,我必须放入x:Name="..."每个网格,这意味着更多的 foreach 循环。

回答by Sivasubramanian

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.

定义一个 Style ,其中TargetType 设置为 Textbox 并且没有 Key。这样,这种样式将应用于应用程序中的所有文本框,而无需为每个文本框绑定样式或前景。

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

回答by Sivasubramanian

Bind all your Textbox's Foreground to a common Brush Resource. Define the brush resource common to Project and access it everywhere.

将所有文本框的前景绑定到一个公共画笔资源。定义Project通用的画笔资源,随处访问。

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]

在 App.XML 中声明画笔资源,以便您可以从项目的任何位置访问它。[注意:你也可以定义它资源字典并参考它]

<Application.Resources>
     <SolidColorBrush  Color="Red" x:Key="txtColor" />
</Application.Resources>

In All your textbox bind the foreground to the "txtColor" brush resource.

在您的所有文本框中,将前景绑定到“txtColor”画笔资源。

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

回答by Inept Adept

Ignore my code and have a look at this answer

忽略我的代码,看看这个答案

Find all controls in WPF Window by type

按类型查找 WPF 窗口中的所有控件

回答by Inept Adept

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>

回答by Bernoulli IT

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 onefor some examples that might help you go the right way ;)

基于控制前景外观的标准文本框创建一个“用户控件”怎么样?这样你就有了一个可重用的控件,你可以在任何你想要的地方使用它,并“完全控制”它的外观和行为。看看这篇文章,或这一个的一些例子,可以帮助你正确的路要走;)