即使在wpf C#中禁用控件时如何获得100%不透明度

时间:2017-07-05 12:12:02

标签: wpf

我的wpf中有一个文本框,当我禁用它时不透明度降低。这恰好告诉用户文本框未启用。有没有办法禁用控件并保持外观相同?

2 个答案:

答案 0 :(得分:1)

Template属性设置为您自己的自定义ControlTemplate

<TextBox IsEnabled="False" Margin="10">
    <TextBox.Template>
        <ControlTemplate TargetType="{x:Type TextBox}">
            <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
            </Border>
            <ControlTemplate.Triggers>
                <!--<Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                        </Trigger>-->
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="true">
                    <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </TextBox.Template>
</TextBox>

答案 1 :(得分:0)

使用IsHitTestVisible="False"代替IsEnabled="False"