Articles in this section

How to Style the TokenItem in WinUI AutoComplete

This article explains how to style the tokenitem in WinUI AutoComplete.

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

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

winui-autocomplete-tokenitemstyle.png

Styling token items based on condition

The AutoComplete control allows you to customize the style of the AutoCompleteTokenItem conditionally based on their 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; }
   public Style DefaultStyle { get; set; }
   protected override Style SelectStyleCore(object item, DependencyObject container)
   {
       if (item is SocialMedia)
       {
           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;
           }
       }
       return DefaultStyle;
   }
}
<Grid>
   <Grid.Resources>
       <Style x:Key="MediaStyle1" TargetType="editors:AutoCompleteTokenItem">
           <Setter Property="Foreground" Value="Blue"/>
           <Setter Property="Background" Value="LightCyan"/>
           <Setter Property="Content" Value="{Binding}"/>
       </Style>
       <Style x:Key="MediaStyle2" TargetType="editors:AutoCompleteTokenItem">
           <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:SfAutoComplete 
       SelectionMode="Multiple"
       ItemsSource="{Binding SocialMedias}"
       TokenItemStyleSelector="{StaticResource socialMediaStyleSelector}"
       DisplayMemberPath="Name"
       TextMemberPath="Name" />
</Grid>	

winui-autocomplete-tokenitemstyleselector.png

Conclusion

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

You can refer to our WinUI AutoComplete 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)
Access denied
Access denied