Click here to Skip to main content
15,887,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a toggle button that will display a different image depending on if it is checked on unchecked. From what I am reading this should work, but it does not.

<Style Selector="ToggleButton.SoundOnOffButton">
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Margin" Value="10"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Style.Resources>
        <Image x:Key="SoundOnImage" Source="/Assets/Sound/icon_sound_alice.bmp" />
        <Image x:Key="SoundOffImage" Source="/Assets/Sound/icon_sound_off_alice.bmp" />
    </Style.Resources>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Image x:Name="ButtonImage" Source="/Assets/Sound/icon_sound_off_alice.bmp" Classes="SetupButtonImage"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style Selector="ToggleButton.SoundOnOffButton:checked /template/ Image#ButtonImage">
    <Setter Property="Source" Value="/Assets/Sound/icon_sound_alice.bmp"/>
</Style>


Here is how it is referenced
<ToggleButton Grid.Row="4" Grid.Column="3"
        Classes="SoundOnOffButton">
</ToggleButton>


What I have tried:

What i have attempted is what is shown in the question above.
Posted
Updated 24-Mar-22 11:34am

1 solution

<Style Selector="ToggleButton.SpeakerTogglebutton:checked" >
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Margin" Value="10"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Image Source="/Assets/Sound/icon_sound_alice.bmp" Classes="SetupButtonImage"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style Selector="ToggleButton.SpeakerTogglebutton:unchecked" >
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Margin" Value="10"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Image Source="/Assets/Sound/icon_sound_off_alice.bmp" Classes="SetupButtonImage"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900