locked
Universal 8.1 App C#: ComboBox With Full Page Selection List Will Not Display Selected Item When First Item Is Selected. Is this a Bug?

    Question

  • When I run the code below and the page is displayed, I select an item from the combobox full page list and it displays fine and then I select the first item from the combobox page list and I do not see that selected item displayed in the box when the selection lists closes.  If I remove the SelectedIndex=0 it all works fine except I do not have an item selected for the initial display.  Below code should show problem and should work copy/paste in universal blank app. Thanks for any help with this problem.

    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()
            {
                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
                {
                }
            }
        }


    Saturday, March 7, 2015 8:10 PM

All replies

  • Hi MV12,

    I modified the implementation of GetTestTypes method to return a simple List<Object> array to make thing simple, and it worked fine no matter whether I set the SelectedIndex property. In short, I cannot reproduce your problem.

    Please try using simple code snippet to test this issue, if you cannot make it work, please submit a repro project on 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.

    Monday, March 9, 2015 7:20 AM
    Moderator
  • Did you try running the code as shown?  I think the problem has something to do with binding to the ObservableCollection<T> that is created from the JSON file.  I have removed the JSON file and I know it works fine.  I need to use the JSON file.
    Monday, March 9, 2015 2:13 PM
  • Hi MV12,

    You’ve not submitted the JSON file, so I get the code snippet from the following link and test in store app, it works. https://code.msdn.microsoft.com/Connection-to-HTTP-and-7505a9a6. This sample use MemoryStream to instead of using OpenStreamForReadAsync method, please try and let me know the result. Note the sample is windows phone, but you can directly copy code snippet into your store app project. And the JSON string is received from web service.

    If you still cannot make it work, please submit your JSON file to help reproduce on my side.

    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, March 10, 2015 6:29 AM
    Moderator
  • The code in my original post for this thread on 3/7 has code that creates a JSON file and then reads the JSON file and that shows the problem.  All you have to do is copy/paste the xaml code and then copy/paste the c# code in a universal blank app and run it and you will see the problem.  The problem occurs with Windows Phone 8.1 emulator and phone.  I am not sure how much simpler I can make it.  I will try changing the OpenStreamForReadAsync to something else and see if that fixes the problem but why would that cause this type of problem?
    Wednesday, March 11, 2015 12:07 AM
  • Hi MV12,

    Sorry for misunderstanding. I’ve tested your code snippet through copy&paste to project in windows and windows phone device, they both worked and I could see the selection of other items.

    Please try this in another computer or windows phone device to check if this will only happen on the particular device.

    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.

    Wednesday, March 11, 2015 7:03 AM
    Moderator
  • The problem is not the selection of other items, it is the selection of the first item from a full page selection list.  Do this: after the initial display with Item1 showing in the combobox, select Item3 or Item4 or any of the items and they will display in the combobox, now select Item1 and you will NOT see it in the combobox display when the selection list closes.  Are you seeing Item1 in the combobox after the selection list closes after you select other items?
    Thursday, March 12, 2015 2:03 PM