I have a requirement where the ComboBox popup should be shown below the ComboBox and the Selected Item should be different i.e the selected item should be shown with an Icon on the left.
To show the ComboBox popup below , I have given the VerticalOffset for the Popup.
<VisualState x:Name="Opened">
<VisualState.Setters>
<Setter Target="Popup.VerticalOffset" Value="30"/>
</VisualState.Setters>
<Storyboard>
<SplitOpenThemeAnimation OpenedTargetName="PopupBorder" ClosedTargetName="ContentPresenter" OffsetFromCenter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownOffset}" OpenedLength="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownOpenedHeight}"/>
</Storyboard>
</VisualState>
But the selected item is not shown in the UI when the popup is opened.
How to fix this?
What you are seeing there is a ContentPresenter
for a placeholder. It seems that this is by design.
So, what you could do is to set the selected item to this ContentPresenter
each time the popup is open.
private void ComboBox_DropDownOpened(object sender, object e)
{
if ((sender as ComboBox)?
.FindDescendant<ContentPresenter>(cp => cp.Name is "ContentPresenter") is not { } contentPresenter)
{
return;
}
contentPresenter.Content = ComboBoxControl.SelectedItem;
FindDescendant()
comes from the CommunityToolkit.WinUI.Extensions NuGet package.