2016-07-18 168 views
2

我遇到了更改禁用文本框顏色的問題。啓用後,我的文本框已具有「前景」屬性。但是我想在禁用時設置另一種顏色。我怎樣才能做到這一點?更改禁用文本框的顏色


@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的顏色進行操作。現在設置爲'藍色'

+0

顯示一些代碼,請的 – Haris

+0

可能的複製[如何改變文本框在WPF中禁用背景色(http://stackoverflow.com/questions/3751990/how- wpf) –

+0

@MichaelMairegger 我的問題適用於Windows 10 UWP。我之前讀過你的文章,Windows 10不支持Triggers – user3688227

回答

0

您需要檢查何時禁用按鈕,然後將顏色設置爲所需的顏色。所以像這樣

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

OR

您還可以設置顏色在礦井的形式加載功能。

所以在表單加載方法中設置顏色。

txt1.button1.BackColor = Color.Red; 

或前景色

txt1.ForeColor = System.Drawing.Color.Red 

沿着這些線。

+0

我看不到任何此屬性的'BackColor','ForeColor'。我正在使用Windwos 10 UWP – user3688227

+1

你能看到這個背景屬性嗎? txt1.Background = new SolidColorBrush(Windows.UI.Colors.White); – Haris

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> 
2

只需編輯文本框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> 
+1

謝謝,我不知道這個功能。我用這個問題有點問題。你能爲我提供一點我的情況嗎?當文本框被禁用時,我想將背景顏色設置爲藍色。何時啓用文字必須更改爲白色。 謝謝 – user3688227