如何只显示按钮图像?

时间:2017-07-24 10:38:33

标签: c# wpf button styles

我有"保存"按钮,它有两种状态:不可见(当没有变化时)和可见:当声音文本改变时。

所以,我创建了XAML:

<Button x:Name="btnSaveText"                                                                               
        Grid.Column="0"                                          
        VerticalAlignment="Center"                                                                          
        HorizontalAlignment="Center"
        Margin="0,0,1,0" Width="22" Height="22" Padding="0"
        BorderBrush="#EFF4FA"
        Background="#EFF4FA"                                                                                                                   IsEnabled="False"                                        
        Style="{StaticResource stlButton}">
        <Image Source="/UI.Resources;component/PNGImages/Save.png" 
               Style="{StaticResource stlButtonImage}" />
</Button>

<Style TargetType="{x:Type Image}" x:Key="stlButtonImage">
    <Setter Property="Margin" Value="1" />
    <Setter Property="HorizontalAlignment" Value="Center" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Stretch" Value="None" />
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" Value="0.5" />
        </Trigger>
    </Style.Triggers>
</Style>

<Style TargetType="{x:Type Button}" x:Key="stlButton">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" Value="0.5" />
            <Setter Property="BorderBrush" Value="#EFF4FA"/>
            <Setter Property="Background" Value="#EFF4FA"/>
        </Trigger>
    </Style.Triggers>
</Style>

但是,当按钮被禁用时,它看起来像这样:

SaveButton

如何仅显示按钮图像?

1 个答案:

答案 0 :(得分:2)

Button的背景设置为Transparent

<Setter Property="Background" Value="Transparent"/>

完整样本:

<Style TargetType="{x:Type Button}" x:Key="stlButton">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" Value="0.5" />

            <Setter Property="BorderBrush" Value="#EFF4FA"/>
            <Setter Property="Background" Value="Transparent"/>
        </Trigger>
    </Style.Triggers>
</Style>

如果您还想摆脱Border,可以将其链接到Background,这样也会隐藏它:

<Style TargetType="{x:Type Button}" x:Key="stlButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
             </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" Value="0.5" />
            <Setter Property="Background" Value="Transparent"/>
        </Trigger>
    </Style.Triggers>
</Style>