none
[UWP]reading combobox value in two comboboxes

    Question

  • I have a Universal Windows app with two comboboxes on MainPage.xaml. I need to be able to read the current content of the comboboxes to direct program flow. Here are the comboboxes;

    <ComboBox x:Name="RadioCategory" HorizontalAlignment="Left" Margin="117,122,0,0" VerticalAlignment="Top" Height="43" Width="158" RenderTransformOrigin="0.173,-3.095" DataContextChanged="RadioCategory_DataContextChanged" SelectionChanged="RadioCategory_SelectionChanged">
                <ComboBoxItem Content="Select Program"/>
                <ComboBoxItem Content="News"/>
                <ComboBoxItem Content="Classic Radio Shows"/>
                <ComboBoxItem Content="Talk Shows"/>
                <ComboBoxItem Content="Sports"/>
                <ComboBoxItem Content="Weather"/>
            </ComboBox>
            <ComboBox x:Name="RadioShows" HorizontalAlignment="Left" Margin="320,122,0,0" VerticalAlignment="Top" Height="43" Width="158" RenderTransformOrigin="0.478,0.884" >
                <ComboBoxItem Content="Select Show"/>
                <ComboBoxItem Content="Show1" IsSelected="True"/>
                <ComboBoxItem Content="Show2"/>
            </ComboBox>

    andi I have the following code in a button for testing,

    try
                {
                     string m_cbvalue = ((ComboBoxItem)RadioCategory.SelectedItem).Content.ToString();
                    string m_cbvalue2    = ((ComboBoxItem)RadioShows.SelectedItem).Content.ToString();

                }
                catch (Exception ex)
                {
                    String m_error2 = ex.ToString();
                }

    String m_cbvalue works fine, but m_cbvalue2 give the error;

    System.InvalidCastException: Unable to cast object of type 'System.String' to type 'Windows.UI.Xaml.Controls.ComboBoxItem

    What going on! There the same!


    Friday, November 2, 2018 9:56 PM

All replies

  • I want the user to use the first combobox RadioCategory to make a selection. Based on that selection I change the Items of the second combobox, RadioShows which the user then selects. I use the following code when the first combobox is changed ;

                         RadioShows.Items.Clear();
                         RadioShows.Items.Add("Item1");
                         RadioShows.Items.Add("Item2");

    Somehow once those three lines of code are run and I check the value of the RadioShows combobox it gives me the error;

    System.InvalidCastException: Unable to cast object of type 'System.String' to type 'Windows.UI.Xaml.Controls.ComboBoxItem

    when I check the value of RadioShows combobox. It apears it changes the object somehow.  How would I change the items of the RadioShows combo and not get the error when I check the value?

    Friday, November 2, 2018 10:32 PM
  • Hi David32,

    Since your issue is related to UWP, but this forum is discussing question about C#, so I will move your thread to UWP forum for professional help.

    Thank you for participating in the forum activities.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 5, 2018 3:03 AM
  • Hello David32,

    Add ComboBoxItems to RadioShows.Items so that the items can be compatible to the original items defined in XAML. Then you'll be able to get m_cbvalue2 properly.

        RadioShows.Items.Clear();
        RadioShows.Items.Add(new ComboBoxItem { Content = "Item1" });
        RadioShows.Items.Add(new ComboBoxItem { Content = "Item2" });

    Monday, November 5, 2018 7:25 AM
  • Hi,

    Well, the problem is that you are adding a string object to the items and force to convert the string object to a ComboBoxItem object when you want to get the content.

    You could try to add ComboBoxItem object to the combobox directly instead of string objects

    Like this:

     private void RadioCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                RadioShows.Items.Clear();
                RadioShows.Items.Add(new ComboBoxItem() { Content = "item1" });
                RadioShows.Items.Add(new ComboBoxItem() { Content = "item22" });
            }

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 5, 2018 7:26 AM
    Moderator
  • Thanks, That fixed my problem!
    Tuesday, November 6, 2018 6:43 PM