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'
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>
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>
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.