如何在WPF中禁用样式为Button.Content的按钮

问题描述 投票:0回答:1

我有一个像这样的按钮:

<Button x:Name="CloseBtn" Click="CloseBtn_Click" IsEnabled="{Binding IsCloseEnabled}" 
                        HorizontalAlignment="Right" Style="{StaticResource TopButton}">
                    <Button.Content>
                        <StackPanel Orientation="Horizontal">
                            <Image Height="30" Width="30" Source="{StaticResource CloseIcon}" />
                            <Label Foreground="Black">Close</Label>
                        </StackPanel>
                    </Button.Content>
                </Button>

它的样式是这样:

<Style x:Key="TopButton" TargetType="{x:Type Button}">
            <Setter Property="Padding" Value="10,5" />
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="TextBlock.Foreground" Value="#FFADADAD"/>
                </Trigger>

            </Style.Triggers>
        </Style>

但是,当绑定“ IsCloseEnabled”返回false时,该按钮看起来并不像被禁用了-颜色“ #FFADADAD”不会应用于前景。不知道哪里出了问题。

wpf wpf-controls
1个回答
0
投票

您的标签的固定值为Foreground =“ Black”,它将覆盖您尝试通过触发器应用的任何其他颜色。删除“ Foreground = Black”,然后查看是否可以解决您的问题。

更新#2:如果您还希望控制图像的显示效果,则最好将图像源设置为用“ disabled” png替换源的样式来控制。像这样的东西:

        <Button x:Name="CloseBtn" Click="CloseBtn_Click" IsEnabled="{Binding IsCloseEnabled}" HorizontalAlignment="Right" Style="{StaticResource TopButton}">
            <Button.Content>
                <StackPanel Orientation="Horizontal">
                    <Image x:Name="myImage" Height="30" Width="30">
                        <Image.Style>
                            <Style TargetType="Image">
                                <Setter Property="Source" Value="{StaticResource CloseIcon}"/>
                                <Style.Triggers>
                                    <Trigger Property="IsEnabled" Value="False">
                                        <Setter Property="Source" Value="{StaticResource CloseIconDisabled}"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                    <Label>Close</Label>
                </StackPanel>
            </Button.Content>
        </Button>

© www.soinside.com 2019 - 2024. All rights reserved.