Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Change Button Image When Enabled/Disabled
Message
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Miscellaneous
Thread ID:
01470823
Message ID:
01470976
Views:
36
>>>>>>>>I put a button on a window and it's disabled. For the image I used a greyed out version of the image. When the button becomes enabled I'd like to swap the image to the enabled pic.
>>>>>>>>
>>>>>>>>Can someone point me in the right direction?
>>>>>>>>
>>>>>>>>Thanks
<Button x:Name="MyButton">
>>>>>>>            <StackPanel>
>>>>>>>                <TextBlock>Do Something</TextBlock>
>>>>>>>                <Image >
>>>>>>>                    <Image.Style>
>>>>>>>                        <Style>
>>>>>>>                            <Style.Triggers>
>>>>>>>                                <DataTrigger Binding="{Binding ElementName=MyButton,Path=IsEnabled}" Value="false">
>>>>>>>                                    <Setter Property="Image.Source" Value="disabledimage.png"/>
>>>>>>>                                </DataTrigger>
>>>>>>>                                <DataTrigger Binding="{Binding ElementName=MyButton,Path=IsEnabled}" Value="true">
>>>>>>>                                    <Setter Property="Image.Source" Value="enabledimage.png"/>
>>>>>>>                                </DataTrigger>
>>>>>>>                            </Style.Triggers>
>>>>>>>                        </Style>
>>>>>>>                    </Image.Style>
>>>>>>>                </Image>
>>>>>>>            </StackPanel>
>>>>>>></Button>
(assuming the image files are in the relevant location and have BuildAction = Resource....)
>>>>>>
>>>
>>>Now I see it. Thanks!
>>>>>>
>>>>>>WOW. Awesome. Thanks!
>>>>>
>>>>>I don't know if you read this before I updated but if so check the current post.
>>>>> I thought of a way of doing this with just one trigger which is a bit more efficient
>>>>
>>>>Ok, I'm not seeing it
>>
>>Duh. I hadn't posted the update when you replied. Try this instead (more efficient):
<Button x:Name="MyButton">
>>            <StackPanel>
>>                <TextBlock>Do Something</TextBlock>
>>                <Image >
>>                    <Image.Style>
>>                        <Style TargetType="Image">
>>                            <Setter Property="Source" Value ="enabledImage.png"/>
>>                            <Style.Triggers>
>>                                <DataTrigger Binding="{Binding ElementName=MyButton,Path=IsEnabled}" Value="false">
>>                                    <Setter Property="Image.Source" Value="disabledImage.png"/>
>>                                </DataTrigger>
>>                            </Style.Triggers>
>>                        </Style>
>>                    </Image.Style>
>>                </Image>
>>            </StackPanel>
>></Button>
>
>
>Ok, here's what I have:
>
>
><Button Name="cmdSave"
>        Command="my:SaveItemCommand.SaveItem"
>        IsEnabled="{Binding IsAppSelected}">
>    <StackPanel Orientation="Horizontal">
>        <Image Stretch="None">
>            <Image.Style>
>                <Style TargetType="Image">
>                    <Setter Property="Source" Value ="C:\Projects\MyApp\Media\Graphics\save_16x16_enabled.png"/>
>                    <Style.Triggers>
>                        <DataTrigger Binding="{Binding ElementName=cmdSave,Path=IsEnabled}" Value="false">
>                            <Setter Property="Image.Source" Value="C:\Projects\MyApp\Media\Graphics\save_16x16_disabled.png"/>
>                        </DataTrigger>
>                    </Style.Triggers>
>                </Style>
>            </Image.Style>
>        </Image>
>        <Label Content="Save" VerticalAlignment="Center"/>
>    </StackPanel>
></Button>
>
>
>I would like to remove the paths from the sources. I'v tried using Source="{StaticResource save_16x16_enabled.png}" but VS complains.
>
>What's the right way to do this?

As in my original post:
Add the png files to the project and make sure the BuildAction is set to resource. This will automatically embed the files in the EXE/DLL. You could create a folder beneath the app folder (e.g 'Images') and place the image files there. In XAML they can then be referenced simply as:
<Setter Property="Image.Source" Value="Images/save_16x16_disabled.png"/>
HTH,
Viv
P.S You could use a TextBlock instead of a Label - lighter weight.....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform