locked
Windows Phone 8.1 xaml, c# ComboBox does not display selection when first item is selected from full page list.

    Question

  • I have 3 comboboxes on a xaml page and most of the time when I select the first item on the full page list displayed when the number of items are > 5, the screen returns to the combobox and the box is blank. The combobox.SelectedItem is valued but it does not display.  This does not happen every time and I have seen this using the emulator and my windows 8.1 phone.  If I select any other item in the list they always display but when I go back and select the first item the box is blank.  Does anyone know what I could be doing that is causing this?
    Friday, January 16, 2015 9:59 PM

All replies

  • If you could post some of your code behind?

    Happy Coding!

    Saturday, January 17, 2015 3:14 PM
  • Sorry for the delayed response but I have been trying to find out what part of the code seems to be causing this. Below is what I have found:  I am binding the combobox to an ObservableCollection<T> where <T> is simple two string objects stored in a local JSON file.  Everything works fine except when I define the SelectedIndex or SelectedItem after the combobox itemssource has been defined with the collection.  Below is the code for the combobox that I have made simple as possible trying to see what is causing this problem and the xaml.cs part behind the xaml.  If I remove the last line where the SelectedIndex is defined, the combobox allows the first item on the full page list to be displayed when selected.  If this line is used, the first item on the list selected from the full page does not display.  I need the last line so I can start with a default value. Thanks for any help.

    <ComboBox  x:Name="comboBoxTestType" ItemsSource="{Binding}" Margin="20,-20,0,0" Width="253">
    <ComboBox.ItemTemplate>
       <DataTemplate>
              <TextBlock Text="{Binding ItemDescription}"/>
         </DataTemplate>
     </ComboBox.ItemTemplate>
    </ComboBox>

    var comboBoxTestTypesItemsSource = await GetTestTypes();

    comboBoxTestType.ItemsSource = comboBoxTestTypesItemsSource;

    comboBoxTestType.SelectedIndex = 0;

    Sunday, January 18, 2015 5:27 PM
  • Hi MV12,

    >> If this line is used, the first item on the list selected from the full page does not display.

    I added some code with the code you provided, but I cannot reproduce your issue, it seems working fine. I used the following code snippet. 

    private void Page_Loaded(object sender, RoutedEventArgs e)
    
            {
    
                var comboBoxTestTypesItemsSource = GetTestTypes();
    
                comboBoxTestType.ItemsSource = comboBoxTestTypesItemsSource;
    
                comboBoxTestType.SelectedIndex = 0;
    
            }
    
    
            private ObservableCollection<ItemDetail> GetTestTypes()
    
            {
    
                ObservableCollection<ItemDetail> list = new ObservableCollection<ItemDetail>();
    
                Random rnd = new Random();
    
                for (int i = 0; i < 50; i++)
    
                {
    
                    list.Add(new ItemDetail() { ItemDescription = "hello" + rnd.Next(0, 1001), ItemName = rnd.Next(0, 101).ToString() });
    
                }
    
                return list;
    
            }
    

    If you still cannot make it working, please post a mini repro project. I am going to see what happened. Use your OneDrive and share a link here.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Tuesday, February 10, 2015 3:42 AM
    Moderator
  • Thanks for your help.  When I run the code below and the page is displayed, I select an item from the page list and it displays fine and then I select the first item from the page list and I do not see that selected item in the box.  If I remove the SelectedIndex=0 it all works fine except I do not have an item selected for the initial display.  Below should show problem and should work copy/paste in universal blank app.

    ScrollViewer x:Name="scrollViewer1">
            <StackPanel x:Name="stackPanel1" Background="Transparent">
                <ComboBox x:Name="comboBoxTestTypes" Margin="20,117.75,0,465.25" SelectedValuePath="ItemDescription" Width="310" Height="Auto" FontSize="17"
           ItemsSource="{Binding}" HorizontalAlignment="Left" VerticalAlignment="Center">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding ItemDescription}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

     </StackPanel>
        </ScrollViewer>

    public sealed partial class MainPage : Page
        {
            public ObservableCollection<ItemDetail> itemDetail;

            public MainPage()
            {
                this.InitializeComponent();
                this.NavigationCacheMode = NavigationCacheMode.Required;
                this.itemDetail = new ObservableCollection<ItemDetail>();
            }

            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                PageLoad();
            }

            public class ItemDetail
            {
                public string ItemDescription { get; set; }
                public string ItemName { get; set; }
            }

            private async void PageLoad()
            {
                await initializeTypes();
                var comboBoxTestTypesItemsSource = await GetTestTypes();
                comboBoxTestTypes.ItemsSource = comboBoxTestTypesItemsSource;
                comboBoxTestTypes.SelectedIndex = 0;
            }

            private async Task initializeTypes()
            {
                await addTestType("Item1");
                await addTestType("Item2");
                await addTestType("Item3");
                await addTestType("Item4");
                await addTestType("Item5");
                await addTestType("Item6");
            }

            private async Task<ObservableCollection<ItemDetail>> GetTestTypes()
            {
                //ObservableCollection<ItemDetail> list = new ObservableCollection<ItemDetail>();
                //Random rnd = new Random();
                //for (int i = 0; i < 50; i++)
                //{
                //    list.Add(new ItemDetail() { ItemDescription = "hello" + rnd.Next(0, 1001), ItemName = rnd.Next(0, 101).ToString() });
                //}

                await getSaveDataFileDataAsync("get");
                return itemDetail;
            }

            private async Task addTestType(string description)
            {
                int newIdAdd = 0;
                if (itemDetail != null && itemDetail.Count > 0)
                {
                    newIdAdd = itemDetail.Max(mT => Convert.ToInt32(mT.ItemName)) + 1;
                }
                var addTestType = new ItemDetail();
                addTestType.ItemName = newIdAdd.ToString();
                addTestType.ItemDescription = description;
                itemDetail.Add(addTestType);
                await getSaveDataFileDataAsync("save");
            }

            private async Task getSaveDataFileDataAsync(string getSave)
            {
                try
                {
                    DataContractJsonSerializer jsonSerializer1 = null;
                    string dataFileName1 = "testFileName";
                    jsonSerializer1 = new DataContractJsonSerializer(typeof(ObservableCollection<ItemDetail>));

                    if (jsonSerializer1 != null && getSave == "get")
                    {
                        using (var stream1 = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(dataFileName1))
                        {
                            itemDetail = (ObservableCollection<ItemDetail>)jsonSerializer1.ReadObject(stream1);
                        }
                    }

                    if (jsonSerializer1 != null && getSave == "save")
                    {
                        using (var stream1 = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(dataFileName1, CreationCollisionOption.ReplaceExisting))
                        {
                                jsonSerializer1.WriteObject(stream1, itemDetail);
                        }
                    }
                }
                catch
                {
                }
            }
        }


                       

    Wednesday, February 18, 2015 9:23 PM
  • I'm having this very same problem.  It does not happen all the time.  I have a ComboBox that is populated with a bunch of items, and sometimes when I select the first item in the list, the ComboBox shows empty, yet the SelectedValue is indeed the item at index 0.  It also can happen if I select the ComboBox with the first item as being the SelectedItem, and then hit the back button.

    Has there been any resolve to this?

    Tuesday, May 19, 2015 2:26 AM
  • Here is a ScreenShot of the issue at hand.  Notice that the "Item:" ComboBox shows empty.  Yet, the SelectedItem is indeed the item that is designated as "[New]"

    When it is working fine, this is what it looks like:

    Tuesday, May 19, 2015 2:33 AM
  • Hey Kevin,

    I never received anything from this list that helped solve this problem.  I think it must be a bug.  The work around I used was to refresh the combobox and make the selectedindex=0 if the first item was selected and the number of items was >5 (which means the full page select list displayed).  This is a very inefficient way of making this work but it was the only way I was able to make this work every time.

    Hope this helps

    Tuesday, May 19, 2015 2:43 PM