2

I have problem with changing color of disabled textbox. I have already 'foreground' property for my textbox when it is enabled. But I want to set another color when it is disabled. How can I achieve this?


@Harry

It's nothing special

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;

    }

By default txt1 is disabled and has grey color. I want change this color.

I can operate only on colors of enabled texbox. Now is set to 'blue'

Liam
  • 27,717
  • 28
  • 128
  • 190
user3688227
  • 123
  • 2
  • 12

3 Answers3

3

You could work with 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;
        }
    }
}

And in XAML

<TextBox>
    <interactivity:Interaction.Behaviors>
        <yourNamespace:ControlBackgroundColorBehavior DisabledForegroundColor ="Red" />
    </interactivity:Interaction.Behaviors>
</TextBox>
Michael Mairegger
  • 6,833
  • 28
  • 41
2

Just edit the Style of your TextBox - you will find there VisualState responsible for changes when control is disabled. You can change foreground, background, border - anything you want. A sample with background color changed to red:

<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>
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • 1
    Thanks, I didn't know about this feature. I have llitle problems with using this. Could You provide me a little in my case? When textbox is disabled, I want to color o background was for example blue. When will enable text has to change to white. Thanks – user3688227 Aug 20 '16 at 12:53
0

You need to check when button is disabled then set the colour to what you need. so something like this

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

OR

You can also set the colour inthe form load function.

so in the form load method set the colour.

txt1.button1.BackColor = Color.Red; 

or forecolor

txt1.ForeColor = System.Drawing.Color.Red

Along these lines.

Harry
  • 3,031
  • 7
  • 42
  • 67