Poma
Poma

Reputation: 8474

How to disable TextBlock?

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.

Upvotes: 39

Views: 21757

Answers (3)

Javert
Javert

Reputation: 279

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.

Upvotes: 14

brunnerh
brunnerh

Reputation: 184296

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>

Upvotes: 58

Zamboni
Zamboni

Reputation: 8043

You can play with Background and apply a SystemColor.
Here is an example to get you started.

<TextBlock IsEnabled="True" 
        Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" 
        Name="textBlock" 
        Text="TEST TextBlock" 
        Height="30" />

Your other option is to try the IsReadOnly property of the TextBox.

Upvotes: 2

Related Questions