Using the StackPanel as ItemsPanelTemplate for ComBoBox occured strange behavior

Xie Steven 831 Reputation points
2020-03-04T07:32:50.383+00:00

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; }  
    }  
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Fay Wang - MSFT 5,221 Reputation points
    2020-03-04T08:07:41.667+00:00

    Hello,

    ​Welcome to our Microsoft Q&A platform!

    This is an expected bebavior. Please refer to the Note part of this document, it mentions:

    > ComboBox uses a CarouselPanel as its ItemsPanel. Using a different panel as the ItemsPanel is not supported and might result in undesired behavior.

    So please don't use StackPanel as its ItemsPanel but CarouselPanel.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.