如何从 ListBox ( ListBoxItems) Windows 获取数据到 string/int 通用
How to get data to string/int from ListBox ( ListBoxItems) Windows Universal
我希望以前没有人问过这样的问题,我做了一些研究但没有一个答案对我有用,所以我决定自己问。
我正在制作一个简单的应用程序,用户必须从列表中选择几个选项,我已经制作了列表框和项目列表。
<ListBox x:Name="Continent">
<ListBoxItem Content="Europe" Foreground="White" />
<ListBoxItem Content="Africa" Foreground="White"/>
<ListBoxItem Content="Asia" Foreground="White"/>
</ ListBox>
我对 Windows Universal Platform 还很陌生,希望我做对了。
无论如何,现在我想在用户按下 "forward" 按钮后将该数据收集到一个字符串中。我试过:
string selected = Continent.SelectedItem.Content;
和
string selected = Continent.SelectedItems[0].Content;
我也尝试添加 "text" 值,但它不允许。
有人知道怎么做吗?最简单的方法是什么?
提前致谢
我认为这里的合理答案是帮助您学习如何自行调试问题,并在学习平台的过程中发现问题。
你的例子很简单。大多数时候,Content 会被设置为 class 的一个实例,就像一个 Continent 对象。然而,你拥有的很好,你只需要使用调试器来了解你正在返回的对象。
为此:var data = Continent.SelectedItem;
,您可以在立即window中看到以下内容:
data is ListBoxItem
true
(data as ListBoxItem).Content
"Africa"
(data as ListBoxItem).Content is string
true
((ListBoxItem)data).Content
"Africa"
遇到问题时在调试器中试一试,使用 Immediate window
,或查看 Locals
以发现返回的类型。
所以你的答案可能是:
string selected = ((ListBoxItem)data).Content
祝你项目顺利。
使用列表框项目的内容不是一个好主意。这些控件有特定的 属性 来包含逻辑数据,它被称为 'tag'。你可以把你想要的任何东西都放在里面,就像复杂的对象一样,并且有不同的显示(内容)值。
因此您必须将 xaml 更改为:
<ListBox x:Name="Continent">
<ListBoxItem Content="Europe" Foreground="White" Tag="europe" />
<ListBoxItem Content="Africa" Foreground="White" Tag="africa"/>
<ListBoxItem Content="Asia" Foreground="White" Tag="asia"/>
</ListBox>
和你的code-behind:
foreach (ListBoxItem selectedItem in Continent.SelectedItems)
{
var continentTag = selectedItem?.Tag as string;
if (continentTag != null)
{
//Do your stuff here
}
}
我希望以前没有人问过这样的问题,我做了一些研究但没有一个答案对我有用,所以我决定自己问。 我正在制作一个简单的应用程序,用户必须从列表中选择几个选项,我已经制作了列表框和项目列表。
<ListBox x:Name="Continent">
<ListBoxItem Content="Europe" Foreground="White" />
<ListBoxItem Content="Africa" Foreground="White"/>
<ListBoxItem Content="Asia" Foreground="White"/>
</ ListBox>
我对 Windows Universal Platform 还很陌生,希望我做对了。
无论如何,现在我想在用户按下 "forward" 按钮后将该数据收集到一个字符串中。我试过:
string selected = Continent.SelectedItem.Content;
和
string selected = Continent.SelectedItems[0].Content;
我也尝试添加 "text" 值,但它不允许。
有人知道怎么做吗?最简单的方法是什么?
提前致谢
我认为这里的合理答案是帮助您学习如何自行调试问题,并在学习平台的过程中发现问题。
你的例子很简单。大多数时候,Content 会被设置为 class 的一个实例,就像一个 Continent 对象。然而,你拥有的很好,你只需要使用调试器来了解你正在返回的对象。
为此:var data = Continent.SelectedItem;
,您可以在立即window中看到以下内容:
data is ListBoxItem
true
(data as ListBoxItem).Content
"Africa"
(data as ListBoxItem).Content is string
true
((ListBoxItem)data).Content
"Africa"
遇到问题时在调试器中试一试,使用 Immediate window
,或查看 Locals
以发现返回的类型。
所以你的答案可能是:
string selected = ((ListBoxItem)data).Content
祝你项目顺利。
使用列表框项目的内容不是一个好主意。这些控件有特定的 属性 来包含逻辑数据,它被称为 'tag'。你可以把你想要的任何东西都放在里面,就像复杂的对象一样,并且有不同的显示(内容)值。
因此您必须将 xaml 更改为:
<ListBox x:Name="Continent">
<ListBoxItem Content="Europe" Foreground="White" Tag="europe" />
<ListBoxItem Content="Africa" Foreground="White" Tag="africa"/>
<ListBoxItem Content="Asia" Foreground="White" Tag="asia"/>
</ListBox>
和你的code-behind:
foreach (ListBoxItem selectedItem in Continent.SelectedItems)
{
var continentTag = selectedItem?.Tag as string;
if (continentTag != null)
{
//Do your stuff here
}
}