如何禁用TextBlock?

Pom*_*oma 29 .net c# wpf xaml

我希望我的TextBlock看起来已禁用(灰显)但当我将IsEnabled属性设置为false时没有任何反应,它会保持黑色:

<TextBlock Text="test" IsEnabled="False" />
Run Code Online (Sandbox Code Playgroud)

这是为什么?

我也尝试使用,Label但由于某种原因它的尺寸更大,所以它会弄乱我的所有布局.

H.B*_*.B. 48

这是使用TextBlock来实现它的正确方法我认为:

<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>
Run Code Online (Sandbox Code Playgroud)


Jav*_*ert 6

我玩了一点,发现半透明度与IsEnabled ="False"的结果相同.

<TextBlock Text="test" Opacity="0.5" />
Run Code Online (Sandbox Code Playgroud)

优点:它适合每种前景色.