Hello,
Welcome to Microsoft Q&A!
It is possible to use DataTemplateSelector in WinUI3 app for the ComboBox. So you could use the DataTemplateSelector to set a Separator like item in the Combobox. I've made a demo in C#, you could refer to the following code and convert the C# code to C++/WinRT. The XAML part should be the same.
Xaml:
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel.Resources>
<DataTemplate x:Key="NormalItemTemplate" x:DataType="x:String">
<StackPanel Background="AliceBlue" Width="300" Height="50">
<TextBlock Text="{x:Bind}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="SeparatorItemTemplate" x:DataType="x:String">
<StackPanel Background="Black" Width="300" Height="15"/>
</DataTemplate>
<local:MyDataTemplateSelector x:Key="MyDataTemplateSelector"
Normal="{StaticResource NormalItemTemplate}"
Separator="{StaticResource SeparatorItemTemplate}"/>
</StackPanel.Resources>
<ComboBox x:Name="FontsCombo" Header="Fonts" Height="88" Width="300"
ItemsSource="{x:Bind fonts}" ItemTemplateSelector="{StaticResource MyDataTemplateSelector}" />
</StackPanel>
MainWindow
public sealed partial class MainWindow : Window
{
public ObservableCollection<string> fonts { get; set; }
public MainWindow()
{
this.InitializeComponent();
fonts= new ObservableCollection<string>();
fonts.Add("Arial");
fonts.Add("1");
fonts.Add("Courier New");
fonts.Add("Times New Roman");
}
}
public class MyDataTemplateSelector : DataTemplateSelector
{
public DataTemplate Normal { get; set; }
public DataTemplate Separator { get; set; }
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (item.Equals("1"))
{
return Separator;
}
else
{
return Normal;
}
}
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.