更改禁用文本框的颜色

本文关键字:颜色 文本 | 更新日期: 2023-09-27 17:57:21

我在更改禁用文本框的颜色时遇到问题。启用文本框时,我已经为它提供了"前景"属性。但是我想在禁用时设置另一种颜色。我怎样才能做到这一点?


@Harry

没什么特别的

.XML

 <TextBox x:Name="txt1" HorizontalAlignment="Left" Margin="98,185,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="160" IsEnabled="False" Background="Blue" />
 <Button x:Name="btn1" Content="Button" HorizontalAlignment="Left" Margin="98,271,0,0" VerticalAlignment="Top" Click="btn1_Click"/>

.CS:

  private void btn1_Click(object sender, RoutedEventArgs e)
    {
        txt1.IsEnabled = true;
    }

默认情况下,txt1 处于禁用状态,并且呈灰色。我想改变这个颜色。

我只能对启用的texbox的颜色进行操作。现在设置为"蓝色"

更改禁用文本框的颜色

你可以使用Behaviors

public class ControlBackgroundColorBehavior : DependencyObject, IBehavior
{
    private Control _contentDialog;
    public void Detach()
    {
        _contentDialog.IsEnabledChanged -= OnIsEnabledChanged;
    }
    DependencyObject IBehavior.AssociatedObject { get; }
    public DependencyObject AssociatedObject { get; private set; }
    public void Attach(DependencyObject associatedObject)
    {
        AssociatedObject = associatedObject;
        _contentDialog = AssociatedObject as ContentDialog;
        _contentDialog.IsEnabledChanged += OnIsEnabledChanged;
    }
    public Brush DisabledForegroundColor { get; set; }
    private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if (!Equals(e.NewValue, true))
        {
            _contentDialog.Foreground= DisabledForegroundColor ;
        }
        else
        {
            _contentDialog.Foreground= (Brush)Control.ForegroundProperty.GetMetadata(typeof(Control)).DefaultValue;
        }
    }
}

在 XAML 中

<TextBox>
    <interactivity:Interaction.Behaviors>
        <yourNamespace:ControlBackgroundColorBehavior DisabledForegroundColor ="Red" />
    </interactivity:Interaction.Behaviors>
</TextBox>

只需编辑文本框的样式 - 您会发现 VisualState 负责禁用控件时的更改。您可以更改前景,背景,边框 - 任何您想要的东西。背景颜色更改为红色的示例:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Disabled">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="HeaderContentPresenter">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}"/>
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundElement">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BorderElement">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}"/>
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledChromeDisabledLowBrush}"/>
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PlaceholderTextContentPresenter">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledChromeDisabledLowBrush}"/>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>

您需要检查按钮何时被禁用,然后将颜色设置为您需要的颜色。 所以像这样的东西

if(txt1.IsEnabled == true){
  //enable colour
}else{
  //set disable colour
}

您还可以在表单加载功能中设置颜色。

所以在形式加载方法中设置颜色。

txt1.button1.BackColor = Color.Red; 

或前彩

txt1.ForeColor = System.Drawing.Color.Red

沿着这些思路。