WPF Text block gray out text
I want to gray out text in t开发者_开发知识库he WPF text block. how do i make it?
Regards Raju
On C#:
textBox.Foreground = Brushes.Gray;
On XAML:
<TextBox Foreground="Gray" />
To disable it (will change background too):
textBox.IsEnabled = false;
The IsEnabled
flag for a textblock
does not grey the text. This post details the differences between textblock
and label. It also shows the XAML to add a trigger on IsEnabled
to grey the text.
You can set the TextBlock.Foreground property to any color (technically, any Brush). If you want it to be grayed out, just set:
<TextBlock Text="Foo" Foreground="Gray" />
If you want it to look "disabled", you can set IsEnabled to false:
<TextBlock Text="Foo" IsEnabled="false" />
TextBlocks do not grayout automaticly when disabled
you can use a style to do this for you
<Style x:Key="DisableEnableTextBlock" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Opacity" Value="1" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value=".5" />
</Trigger>
</Style.Triggers>
</Style>
The trouble with using the TextBox is that there's a box round it. If you use Label (with Content="Foo") then you can toggle the text colour with IsEnabled. Otherwise it behaves like TextBlock for a short heading/label.
For WinUI set the Opacity property to something around 0.5.
Use TextBox instead and set IsReadOnly = true
or IsEnabled = false
精彩评论