Quantumpencil
Quantumpencil

Reputation: 295

I can't switch between buttons using the style tag (XAML)

Here is my code for a xaml/c# contract I'm working on (I'm a new programmer, but I spent a lot of time proofing this). I've searched SO for help and ended up coming with the following code, which should work and which compiles fine, but still my buttons aren't changing to the disabled art asset when they IsEnabled is set to False.

Here is my Code

<Window x:Class="My_Project.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MyProject" Height="654" Width="943">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Rectangle Grid.Row="0" Grid.Column="1" Fill="#58585A" Margin="0.4,0,-0.2,-0.2"/>
        <StackPanel Grid.Row="0" Grid.Column="0" VerticalAlignment="Top">
            <Image Source="img\company_logo_full.png" Width="200" HorizontalAlignment="Left" Margin="20,0,0,0"></Image>
            <Separator Height="35" Margin="20,0,20,0"/>
            <Button x:Name="NewButton" Click="NewButton_Click" Margin="20,0,0,0" Width="208" Height="35" HorizontalAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Background="{x:Null}" >
                <Image Source="img\new_button.png"  Width="208" Height="35"></Image>
            </Button>
            <Button x:Name="OpenButton" Click="OpenButton_Click" Margin="20,0,0,0" Width="208" Height="35" HorizontalAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Background="{x:Null}" >
                <Image Source="img\open_button.png"  Width="208" Height="35">
                    <Image.Style>
                        <Style TargetType="Image">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="True">
                                    <Setter Property="Source" Value="img\open_button.png" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="False">
                                    <Setter Property="Source" Value="img\open_button_disabled.png" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Image.Style>
                </Image>
            </Button

Thank you very much for your time, ~QP

Upvotes: 0

Views: 47

Answers (1)

Mohd Ahmed
Mohd Ahmed

Reputation: 1482

If you set the properties locally the trigger will not be able to change the value due to precedence. Move the Source property into the style: Try this

 <Image Width="208" Height="35">
                        <Image.Style>
                            <Style TargetType="Image">
 <Setter Property="Source" Value="img\open_button.png" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="True">
                                        <Setter Property="Source" Value="img\open_button.png" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="False">
                                        <Setter Property="Source" Value="img\open_button_disabled.png" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>

Upvotes: 1

Related Questions