tags:

views:

311

answers:

1

Hi, I'm trying to show a different image when the button is disabled. Should be easy with triggers, right?!

For some reason, I have not been able to get the image source to switch to the disabled image when the button is disabled. I've tried setting triggers on both the image and button. What is wrong with what I have below? How can I change the image source when the button is enabled/disabled?

Thanks!

<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>
+4  A: 

Yeah this one pops up quite a bit.

Any property that's explicitly set in the object's declaration can't be changed in a style. So because you've set the image's Source property in the declaration of the image, the style's Setter won't touch it.

Try this instead:

<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>
Matt Hamilton
I'll give that a try- Thanks for quick response, plus the explanation and example!
Taylor