简体   繁体   中英

Changing an icon style for a toggle button in xaml

I am trying to change the icon used for a toggle button depending on what state isChecked is, however the only examples i can find of how to do this use an image to display the icon.

I however, have static icon styles rather than images so i am wondering how I can do this.

As a starting point if I create my toggle botton like this:

 <ToggleButton Style="{StaticResource ToggleButtonStyle}">                             
     <Path Style="{StaticResource Test1IconStyle}"  HorizontalAlignment="Right" />                
 </ToggleButton>

then it displays with the ToggleButton style and the correct Icon.

So now lets say i click the button, i need to reset the 'Path' to 'Test2IconStyle' based on the isChecked value.

Is this possible?

Instead of toggling Style, I would suggest to have two separate Paths in your resources and based on condition you can toggle them like this:

<StackPanel>
    <StackPanel.Resources>
        <Path x:Key="Test1"/>
        <Path x:Key="Test2"/>
    </StackPanel.Resources>
    <ToggleButton>
        <ToggleButton.Style>
            <Style TargetType="ToggleButton"
                   BasedOn="{StaticResource ToggleButtonStyle}">
                <Setter Property="Content" Value="{StaticResource Test1}"/>
                <Style.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Content"
                                Value="{StaticResource Test2}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ToggleButton.Style>
    </ToggleButton>
</StackPanel>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM