[UWP]读取两个组合框中的组合框值

人气:350 发布:2022-09-22 标签: wpdevelop

问题描述

我在MainPage.xaml上有一个带有两个组合框的通用Windows应用程序。我需要能够读取组合框的当前内容以指导程序流程。以下是组合框;

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;

andi我在测试按钮中有以下代码,

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

字符串m_cbvalue工作正常,但m_cbvalue2给出错误;

String m_cbvalue works fine, but m_cbvalue2 give the error;

System.InvalidCastException:无法转换对象键入'System.String'以键入'Windows.UI.Xaml.Controls.ComboBoxItem

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

发生了什么!有相同的!

What going on! There the same!

推荐答案

我希望用户使用第一个组合框RadioCategory进行选择。根据该选择,我更改用户随后选择的第二个组合框,RadioShows的项目。当第一个组合框更改时,我使用以下代码;

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");

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

不知何故,一旦这三行代码运行,我检查了值RadioShows组合框它给我错误;

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:无法将'System.String'类型的对象强制转换为'Windows.UI.Xaml.Controls.ComboBoxItem

当我检查RadioShows组合框的值时。它会以某种方式改变对象。当我检查值时,如何更改RadioShows组合的项目而不会出现错误?

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?

881