更改已禁用文本框的颜色

时间:2016-07-18 09:07:04

标签: c# xaml win-universal-app

我在更改已禁用文本框的颜色方面遇到问题。我已经'前景'启用时我的文本框的属性。但我想在禁用时设置另一种颜色。我怎样才能做到这一点?

@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的颜色进行操作。现在设置为&#39; blue&#39;

3 个答案:

答案 0 :(得分:3)

您可以使用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>

答案 1 :(得分:2)

只需编辑 TextBox the Style - 您就会发现 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>

答案 2 :(得分:0)

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

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

OR

您还可以在表单加载函数中设置颜色。

所以在表单加载方法中设置颜色。

txt1.button1.BackColor = Color.Red; 

或forecolor

txt1.ForeColor = System.Drawing.Color.Red

沿着这些方向。