[英]Change color of disabled textbox
我在更改禁用文本框的顏色時遇到問題。 啟用后,我的文本框已經具有“前景”屬性。 但是我想在禁用它時設置另一種顏色。 我該如何實現?
@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>
只需編輯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
遵循這些原則。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.