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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Applying style for VisualStateManager in SfButton

Thread ID:

Created:

Updated:

Platform:

Replies:

144218 Apr 25,2019 08:16 AM UTC Apr 25,2019 12:30 PM UTC Xamarin.Forms 1
loading
Tags: SfButton
Dr. Arno Pernozzoli
Asked On April 25, 2019 08:16 AM UTC

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

Bharathiraja K [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon