I want to gray out text in the 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With