Articles in this section
Category / Section

How to customize the AccordionButton?

1 min read

You can customize the AccordionButtonStyle property in the SfAccordionitem by defining a new style in the ItemContainerStyle. In the following example, the background and foreground of the SfAccordionItem header are modified through the AccordionButtonStyle and new keys MouseHoverStroke, ExpandedStroke and CollapsedStroke are defined where you can change the color.

Mainpage.xaml

<Page
    x:Class="SfAccordion_Customized.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SfAccordion_Customized "
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:layout="using:Syncfusion.UI.Xaml.Controls.Layout"
    mc:Ignorable="d" Background="Black">
    <Page.Resources>
         <SolidColorBrush x:Key="MouseHoverStroke">#FF90EE90</SolidColorBrush>
        <SolidColorBrush x:Key="CollapsedStroke">#FF006400</SolidColorBrush>
        <SolidColorBrush x:Key="ExpandedStroke">#FF800080</SolidColorBrush>        <Style TargetType="layout:LayoutTransformer" x:Key="layoutStyle">
            <Setter Property="Foreground" Value="#FF000000"/>
            <Setter Property="FontFamily" Value="Segoe UI Symbol"/>
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="Height" Value="30"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="layout:LayoutTransformer">
                        <Grid x:Name="TransformRoot" Background="{TemplateBinding Background}">
                            <ContentPresenter
                            x:Name="Presenter" VerticalAlignment="Center"
                             FontFamily="{TemplateBinding FontFamily}" 
                                        FontSize="{TemplateBinding FontSize}" 
                                        FontStretch="{TemplateBinding FontStretch}" 
                                        FontStyle="{TemplateBinding FontStyle}" 
                                        FontWeight="{TemplateBinding FontWeight}"
                            Content="{TemplateBinding Content}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="layout:AccordionButton" x:Key="accordionButtonStyle">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontFamily" Value="Segoe UI Symbol"/>
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Stretch"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="IsTabStop" Value="True"/>
            <Setter Property="TabNavigation" Value="Once"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="layout:AccordionButton">
                        <Grid Background="Transparent">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="ExpandDirectionStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0"/>
                                    </VisualStateGroup.Transitions>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="ExpansionStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0"/>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Collapsed">
                                        <Storyboard>
                                            <DoubleAnimation BeginTime="00:00:00" Duration="00:00:00.3" Storyboard.TargetName="icon" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" To="0"/>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="arrow">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource CollapsedStroke}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="header">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource CollapsedStroke}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Expanded">
                                        <Storyboard>
                                            <DoubleAnimation BeginTime="00:00:00" Duration="00:00:00.3" Storyboard.TargetName="icon" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" To="90"/>
                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="ExpandedBackground" Storyboard.TargetProperty="(UIElement.Opacity)">
                                                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                                            </DoubleAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="arrow">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ExpandedStroke}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="header">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ExpandedStroke}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="CheckStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="00:00:00"/>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Checked"/>
                                    <VisualState x:Name="Unchecked"/>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="MouseOverBackground"/>
                                            <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="NormalBackground"/>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="#FFEDEDED"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard/>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Focused">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">
                                                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.385"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Unfocused"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="background" Background="{TemplateBinding Background}"
                                >
                                <Grid>
                                    <Border x:Name="NormalBackground"
                                        VerticalAlignment="Stretch"  
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}"/>
                                    <Grid  x:Name="ExpandedBackground" Opacity="0">
                                        <Border 
                                        VerticalAlignment="Stretch" Background="{Binding AccentBrush,RelativeSource={RelativeSource TemplatedParent}}" 
                                        BorderBrush="{TemplateBinding BorderBrush}" 
                                        BorderThickness="{TemplateBinding BorderThickness}"/>
                                    </Grid>
                                    <Grid x:Name="MouseOverBackground"  Opacity="0" >
                                        <Border 
                                        VerticalAlignment="Stretch" Background="{StaticResource MouseHoverStroke}" 
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}"/>
                                    </Grid>
                                    <Grid Background="Transparent" Margin="{TemplateBinding Padding}">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" x:Name="cd0"/>
                                            <ColumnDefinition Width="Auto" x:Name="cd1"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" x:Name="rd0"/>
                                            <RowDefinition Height="Auto" x:Name="rd1"/>
                                        </Grid.RowDefinitions>
                                        <Grid Height="19" Margin="5,0,5,0" Background="Transparent" HorizontalAlignment="Center" x:Name="icon" VerticalAlignment="Center" Width="19" RenderTransformOrigin="0.5,0.5" Grid.Column="1" Grid.Row="0">
                                            <Grid.RenderTransform>
                                                <TransformGroup>
                                                    <ScaleTransform/>
                                                    <SkewTransform/>
                                                    <RotateTransform Angle="-90"/>
                                                    <TranslateTransform/>
                                                </TransformGroup>
                                            </Grid.RenderTransform>
                                            <Path Data="F1M568.254,-7.43524L568.159,-7.34277 567.977,-7.52246 546.559,-28.9417 525.484,-28.9417 549.787,-4.63446 557.439,3.01532 557.619,3.19629 550.067,10.7549 525.758,35.0583 546.834,35.0583 568.254,13.6429 578.792,3.10254 568.254,-7.43524z" 
                                          Stretch="Uniform" Fill="{StaticResource ExpandedStroke}" 
                                          Width="12" Height="12" x:Name="arrow" RenderTransformOrigin="0.5,0.5" 
                                           HorizontalAlignment="Center" VerticalAlignment="Center">
                                                <Path.RenderTransform>
                                                    <TransformGroup>
                                                        <ScaleTransform/>
                                                        <SkewTransform/>
                                                        <RotateTransform/>
                                                        <TranslateTransform/>
                                                    </TransformGroup>
                                                </Path.RenderTransform>
                                            </Path>
                                        </Grid>
                                        <layout:LayoutTransformer
                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                        Style="{StaticResource layoutStyle}"
                                        x:Name="header" 
                                        Grid.Column="0"
                                        Grid.Row="0" 
                                        Grid.RowSpan="1" 
                                        Foreground="{TemplateBinding Foreground}"
                                        FontFamily="{TemplateBinding FontFamily}"
                                                                                                    FontSize="{TemplateBinding FontSize}"
                                                                                                    FontStretch="{TemplateBinding FontStretch}"
                                                                                                    FontStyle="{TemplateBinding FontStyle}"
                                                                                                    FontWeight="{TemplateBinding FontWeight}"
                                        Content="{TemplateBinding Content}" 
                                        ContentTemplate="{TemplateBinding ContentTemplate}"/>
                                    </Grid>
                                </Grid>
                            </Border>
                            <Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Visibility="Collapsed"
                                   RadiusX="1" RadiusY="1" Stroke="#FF6DBDD1" StrokeThickness="1"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="layout:SfAccordionItem" x:Key="accordionitems">
            <Setter Property="AccordionButtonStyle" Value="{StaticResource accordionButtonStyle}"/>
        </Style>
    </Page.Resources>
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<layout:SfAccordion ItemContainerStyle="{StaticResource accordionitems}" AccentBrush="#FFDB7093"
                            VerticalAlignment="Center" Width="450">
            <layout:SfAccordion.ContentTemplate>
                <DataTemplate>
                    <Border Height="150" Width="450">
                        <Grid Background="#FFF4F3F2">
                            <RichTextBlock VerticalAlignment="Top"  TextWrapping="Wrap"
                                           Margin="20" FontSize="14" FontWeight="Light" >
                                <Paragraph LineHeight="20">
                                    <Run FontFamily="Global User Interface">
                                        Essential Studio contains all the controls required to build 
                                        line-of-business applications including charts, gauges, maps, editors, and  much more.
                                    </Run>
                                </Paragraph>
                            </RichTextBlock>
                        </Grid>
                    </Border>
                </DataTemplate>
            </layout:SfAccordion.ContentTemplate>
            <layout:SfAccordionItem Header="Winrt"/>
            <layout:SfAccordionItem Header="Windows Phone"/>
            <layout:SfAccordionItem Header="WPF"/>
            <layout:SfAccordionItem Header="Universal"/>
        </layout:SfAccordion>        
    </Grid>
</Page>

The following screenshot displays the SfAccordion with the customized AccordionButton.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied