Dante
Dante

Reputation: 3316

Change Button Image Issue

Im working in WPF and I am trying to change the image of a button, I have created some triggers and Setters, but it is not working.

Here's my XAML code:

<Button BorderThickness="0" Height="23" HorizontalAlignment="Left" Margin="59,6,0,0" Name="topButton" Width="26"  VerticalAlignment="Top"  
            Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" IsEnabled="False" >
        <StackPanel>
            <Image>
                <Image.Style>
                    <Style TargetType="Image">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="True">
                                <Setter Property="Image.Source" Value="Images/MoveFirst_Enabled.bmp" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="False">
                                <Setter Property="Image.Source" Value="Images/MoveFirst_Disabled.bmp" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>
        </StackPanel>
    </Button>

I cant realize what I am doing wrong. Hope you can help me. Thank you in advance.

Upvotes: 2

Views: 1182

Answers (2)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84647

Remove the Button. for the Path. So instead of

Path=Button.IsEnabled

you should have

Path=IsEnabled

Example

<Button BorderThickness="0" Height="23" HorizontalAlignment="Left" Margin="59,6,0,0" Name="topButton" Width="26"  VerticalAlignment="Top"               Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" IsEnabled="False">
    <StackPanel>
        <Image>
            <Image.Style>
                <Style TargetType="Image">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=topButton, Path=IsEnabled}" Value="True">
                            <Setter Property="Source" Value="Images/MoveFirst_Enabled.bmp" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=topButton, Path=IsEnabled}" Value="False">
                            <Setter Property="Source" Value="Images/MoveFirst_Disabled.bmp" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
    </StackPanel>
</Button>

Upvotes: 3

David Bekham
David Bekham

Reputation: 2175

I think you are not using the correct Uri string for the Source of the Image.

<Button BorderThickness="0" Height="23" HorizontalAlignment="Left" Margin="59,6,0,0" Name="topButton" Width="26"  VerticalAlignment="Top"  
                Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" IsEnabled="False" >
            <StackPanel>
                <Image>
                    <Image.Style>
                        <Style TargetType="Image">

                                <!-- If you are using the Image as a Resource, you should use the following format.-->

                            <Style.Triggers>    
                                <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="True">
                                    <Setter Property="Image.Source" Value="assemblyname;component/Images/MoveFirst_Enabled.bmp" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="False">
                                    <Setter Property="Image.Source" Value="assemblyname;component/Images/MoveFirst_Disabled.bmp" />
                                </DataTrigger>
                            </Style.Triggers>

                           <!-- If you are using the Image as Content then you should use the below format.-->

                            <Style.Triggers>    
                                <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="True">
                                    <Setter Property="Image.Source" Value="Images/MoveFirst_Enabled.bmp" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="False">
                                    <Setter Property="Image.Source" Value="Images/MoveFirst_Disabled.bmp" />
                                </DataTrigger>
                            </Style.Triggers>

                        </Style>
                    </Image.Style>
                </Image>
            </StackPanel>
        </Button>

I hope it may help you...

Upvotes: 1

Related Questions