Articles in this section
Category / Section

How to Style Token Item in WinUI ComboBox

4 mins read

This article explains how to style token items in WinUI ComboBox.

The ComboBox control allows you to customize the style of ComboBoxTokenItem generated in the selection area using the TokenItemStyle property. The default value of TokenItemStyle is null.

XAML
<editors:SfComboBox SelectionMode="Multiple"
                   MultiSelectionDisplayMode="Token"
                   ItemsSource="{Binding SocialMedias}"
                   DisplayMemberPath="Name"
                   TextMemberPath="Name">
           <editors:SfComboBox.TokenItemStyle>
               <Style TargetType="editors:ComboBoxTokenItem">
                   <Setter Property="Foreground" Value="Red"/>
                   <Setter Property="Background" Value="LightCyan"/>
               </Style>
           </editors:SfComboBox.TokenItemStyle>
</editors:SfComboBox>

winui-combobox-tokenitemstyle.png

Style token items based on condition

The ComboBox control allows you to customize the style of ComboBoxTokenItem conditionally based on its content using the TokenItemStyleSelector property. The default value of TokenItemStyleSelector is null.

C#
public class SocialMediaStyleSelector : StyleSelector
{
   public Style MediaStyle1 { get; set; }
   public Style MediaStyle2 { get; set; }
   protected override Style SelectStyleCore(object item, DependencyObject container)
   {
       var mediaItem = (item as SocialMedia).Name;
       if (mediaItem.ToString() == "Facebook" || mediaItem.ToString() == "Instagram" ||
           mediaItem.ToString() == "Twitter" || mediaItem.ToString() == "WhatsApp" ||
           mediaItem.ToString() == "Skype")
       {
           return MediaStyle1;
       }           
       else
       {
           return MediaStyle2;
       }
   }
}
XAML
<Grid>
   <Grid.Resources>
       <Style x:Key="MediaStyle1" TargetType="editors:ComboBoxTokenItem">
           <Setter Property="Foreground" Value="Blue"/>
           <Setter Property="Background" Value="LightCyan"/>
           <Setter Property="Content" Value="{Binding}"/>
       </Style>
       <Style x:Key="MediaStyle2" TargetType="editors:ComboBoxTokenItem">
           <Setter Property="Foreground" Value="Red"/>
           <Setter Property="Background" Value="LightCyan"/>
           <Setter Property="Content" Value="{Binding}"/>
       </Style>
       <local:SocialMediaStyleSelector x:Key="socialMediaStyleSelector" 
                                       MediaStyle1="{StaticResource MediaStyle1}"
                                       MediaStyle2="{StaticResource MediaStyle2}"/>
   </Grid.Resources>
   <editors:SfComboBox SelectionMode="Multiple"                    
                       MultiSelectionDisplayMode="Token"
                       ItemsSource="{Binding SocialMedias}"
                       TokenItemStyleSelector="{StaticResource socialMediaStyleSelector}"
                       DisplayMemberPath="Name"
                       TextMemberPath="Name" />
</Grid>

winui-combobox-tokenitemstyleselector.png

Conclusion

I hope you enjoyed learning about how to add Auto-Append UI in WinUI ComboBox control.

You can refer to our WinUI ComboBox feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the Licence and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forum, Support portal, or feedback portal. We are always happy to assist you!

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