As we all know, the UWP ComboBox control uses the CarouselPanel as its ItemsPanelTemplate. With it, the ComboBox's dropdown-list will cyclic scroll when the app runs on the tablet mode. But we don't want the cyclic scrolling on tablet mode.
So, we use the StaclPanel as its ItemsPanelTemplate. (Here, we do not think about the perfamce issue when using the StackPanel.)
Then, the strange behavior occurred. Once I set an item as the default selected item for the ComBoBox, the first item in the dropdown-list cannot be selected.
See the code sample:
<ComboBox ItemsSource="{x:Bind tests, Mode=OneWay}" SelectedItem="{x:Bind SelectedTest, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="local:Test">
<TextBlock Text="{x:Bind name, Mode=OneWay}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
public sealed partial class MainPage : Page,INotifyPropertyChanged
{
ObservableCollection<Test> tests { get; set; } = new ObservableCollection<Test>();
private Test selectedTest;
public event PropertyChangedEventHandler PropertyChanged;
public Test SelectedTest
{
get
{
return this.selectedTest;
}
set
{
if (this.selectedTest != value)
{
this.selectedTest = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedTest"));
}
}
}
public MainPage()
{
this.InitializeComponent();
for (int i=0;i<10;i++)
{
tests.Add(new Test() {name="name " +i });
}
this.SelectedTest = this.tests[0];
}
}
public class Test
{
public string name { get; set; }
}