Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable TextBlock?

Tags:

c#

.net

wpf

xaml

I want my TextBlock to look disabled (grayed out) but when I set IsEnabled property to false nothing happens, it stays black:

<TextBlock Text="test" IsEnabled="False" /> 

Why is that?

Also I tried to use Label but it's size is bigger for some reason, so it will mess up all my layout.

like image 669
Poma Avatar asked May 02 '11 02:05

Poma


People also ask

What is TextBlock?

Text block is the primary control for displaying read-only text in apps. You can use it to display single-line or multi-line text, inline hyperlinks, and text with formatting like bold, italic, or underlined.

Is TextBlock editable?

TextBlock is not editable.

What is TextBlock WPF?

The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.


2 Answers

This would be the proper way to do it with a TextBlock i think:

<TextBlock Text="Lorem ipsum dolor sit">     <TextBlock.Style>         <Style TargetType="{x:Type TextBlock}">             <Style.Triggers>                 <Trigger Property="IsEnabled" Value="False">                     <Setter Property="Foreground"                             Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}"/>                 </Trigger>             </Style.Triggers>         </Style>     </TextBlock.Style> </TextBlock> 
like image 197
H.B. Avatar answered Sep 28 '22 00:09

H.B.


I played a little and found out that half opacity is giving the same resultat as IsEnabled="False".

<TextBlock Text="test" Opacity="0.5" /> 

Advantage : it fits to every Foreground color.

like image 30
Javert Avatar answered Sep 28 '22 00:09

Javert