We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Applying style for VisualStateManager in SfButton

I'm currently using an SfButton with visual states in order to create a toggle button, which works without problems.
As I need this often within my app, I'm currently trying to introduce a style for that purpose. Therefore I've defined the style within the resource dictionary that way:
[code]
            <Style x:Key="ToggleButton" TargetType="buttons:SfButton">
                <Setter Property="VisualStateManager.VisualStateGroups">
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Pressed">
                            <VisualState.Setters>
                                <Setter Property="TextColor" Value="White" />
                                <Setter Property="BackgroundColor" Value="{StaticResource buttonText}" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Checked">
                            <VisualState.Setters>
                                <Setter Property="TextColor" Value="White" />
                                <Setter Property="BackgroundColor" Value="{StaticResource buttonText}" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Normal">
                            <VisualState.Setters>
                                <Setter Property="TextColor" Value="{StaticResource buttonText}" />
                                <Setter Property="BackgroundColor" Value="{StaticResource header}" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Unchecked">
                            <VisualState.Setters>
                                <Setter Property="TextColor" Value="{StaticResource buttonText}" />
                                <Setter Property="BackgroundColor" Value="{StaticResource header}" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </Setter>
            </Style>
[/code]

Applying this style to the SfButton doesn't work as expected, style is not applied to the button as it is when I define the visual states within the button itself:

[code]
            <buttons:SfButton x:Name="ComposerButton" Text="Composer" Style="{StaticResource ToggleButton}" IsChecked="{Binding ComposerChecked}" Command="{Binding ShowComposerList}">
            </buttons:SfButton>
[/code]

Am I doing something wrong or is this just not possible with SfButton?

Thank you
Arno

1 Reply

BK Bharathiraja K Syncfusion Team April 25, 2019 12:30 PM UTC

Hi Arno, 
 
Thanks for contacting Syncfusion Support. 
 
We have checked your provided code example. We suggest you use VisualStateGroupList instead of VisualStateGroup. The sample is working fine with VisualStateGroupList. Please refer the below code snippet 
 
[XAML]: 
  <ResourceDictionary>
             <Style x:Key="ToggleButton" TargetType="buttons:SfButton">
                <Setter Property="VisualStateManager.VisualStateGroups">
                      <VisualStateGroupList x:Name="CommonStates">
                        <VisualStateGroup>
                             <VisualState x:Name="Pressed">
                            <VisualState.Setters>
                                <Setter Property="TextColor" Value="Black" />
                                <Setter Property="BackgroundColor" Value="White" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Checked">
                            <VisualState.Setters>
                                <Setter Property="TextColor" Value="Red" />
                                <Setter Property="BackgroundColor" Value="Blue" />
                            </VisualState.Setters>
                        </VisualState>
                    <VisualState x:Name="Normal">
                            <VisualState.Setters>
                                <Setter Property="TextColor" Value="White" />
                                <Setter Property="BackgroundColor" Value="Black" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Unchecked">
                            <VisualState.Setters>
                                <Setter Property="TextColor" Value="Blue" />
                                <Setter Property="BackgroundColor" Value="Red" />
                            </VisualState.Setters>
                        </VisualState>
                        </VisualStateGroup>
                    </VisualStateGroupList>
                </Setter>
            </Style>
        </ResourceDictionary>
 
 
Based on the code examples, we have prepared a simple sample for your reference. Please download the sample from below link 
 
Note: For making the Toggle type of SfButton, need to enable the IsCheckable property.  
 
IsCheckable true, then it will be in any of "Checked " and "UnChecked" State. 
 
IsCheckable false, then it will be in any of "Pressed" and "Normal" State 
 
For more information, please refer our User Guide 
 
 
 
Regards, 
Bharathi. 


Loader.
Live Chat Icon For mobile
Up arrow icon