【问题标题】:How do I change the image when the button is disabled?禁用按钮时如何更改图像?
【发布时间】:2011-02-19 08:32:19
【问题描述】:

当按钮被禁用时,我试图显示不同的图像;我认为使用触发器会很容易。

但是,当按钮被禁用时,我无法让图像源切换到禁用的图像。我尝试在图像和按钮上设置触发器。我在下面有什么问题?启用/禁用按钮时如何更改图像源?

<Button
         x:Name="btnName"
         Command="{Binding Path=Operation}"
         CommandParameter="{x:Static vm:Ops.OpA}">
            <Button.Content>
                <StackPanel>
                    <Image
                  Width="24"
                  Height="24"             
                  RenderOptions.BitmapScalingMode="NearestNeighbor"
                  SnapsToDevicePixels="True"
                  Source="/MyAssembly;component/images/enabled.png">
                        <Image.Style>
                            <Style>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=btnName, Path=Button.IsEnabled}" Value="False">
                                        <Setter Property="Image.Source" Value="/MyAssembly;component/images/disabled.png" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                </StackPanel>
            </Button.Content>
        </Button>

【问题讨论】:

    标签: wpf image button


    【解决方案1】:

    是的,这个弹出了不少。

    在对象的声明中明确设置的任何属性都不能在样式中更改。所以因为你在图片的声明中设置了图片的 Source 属性,所以样式的 Setter 不会去触碰它。

    试试这个:

    <Image
        Width="24"  
        Height="24"               
        RenderOptions.BitmapScalingMode="NearestNeighbor"  
        SnapsToDevicePixels="True"
        >
        <Image.Style>
            <Style TargetType="Image">
                <Setter Property="Source"
                        Value="/MyAssembly;component/images/enabled.png" />
                <Style.Triggers>
                    ... your trigger and setter ...
                </Style.Triggers>
            </Style>
        </Image.Style>
    </Image>
    

    【讨论】:

    • 我会试一试-感谢您的快速响应,以及解释和示例!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 2011-12-04
    • 2013-07-24
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多