Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从多个ListBoxItems获取内容并转换为字符串_C#_Xaml_Listbox_Microsoft Metro_Windows 8.1 - Fatal编程技术网

C# 从多个ListBoxItems获取内容并转换为字符串

C# 从多个ListBoxItems获取内容并转换为字符串,c#,xaml,listbox,microsoft-metro,windows-8.1,C#,Xaml,Listbox,Microsoft Metro,Windows 8.1,我花了很长时间在谷歌上搜索stackoverflow,但要么解决方案不是我想要的,要么我的实现不太正确。不要认为这是一个大问题,所以希望有人能提供一个解决方案(有点小问题,所以需要更多的细节让我知道) 我试图从W8.1 universal XAML应用程序中的列表框中获取多个项目的值,以便将它们传递给SQLite DB(我认为是使用逗号分隔的“join”语句的字符串)。ListItems的数据源当前为各种值手动设置 同时,我正在为字符串的文本设置一个标签作为测试,在列表框中使用复选框并没有带来任

我花了很长时间在谷歌上搜索stackoverflow,但要么解决方案不是我想要的,要么我的实现不太正确。不要认为这是一个大问题,所以希望有人能提供一个解决方案(有点小问题,所以需要更多的细节让我知道)

我试图从W8.1 universal XAML应用程序中的列表框中获取多个项目的值,以便将它们传递给SQLite DB(我认为是使用逗号分隔的“join”语句的字符串)。ListItems的数据源当前为各种值手动设置

同时,我正在为字符串的文本设置一个标签作为测试,在列表框中使用复选框并没有带来任何乐趣,因此我改为ListBoxItems,但是我没有通过它来获取内容,只执行以下操作(当选择两个列表项时):

Windows.UI.Xaml.Controls.ListBoxItem, Windows.UI.Xaml.Controls.ListBoxItem

很明显,我想要的是内容,应该是“开发者”和“测试者”。下面是列表框的XAML和我的当前代码:

<TextBlock x:Name="lblAreas" HorizontalAlignment="Left" Margin="548,200,0,0" TextWrapping="Wrap" Text="Areas of Interest" VerticalAlignment="Top" RenderTransformOrigin="1.143,1.692" FontSize="13.333" Height="32" Width="336"/>
    <ListBox x:Name="lbAreas" HorizontalAlignment="Left" Height="231" Margin="548,241,0,0" VerticalAlignment="Top" Width="194" SelectionMode="Multiple">
        <ListBoxItem Content="Developer"/>
        <ListBoxItem Content="Tester"/>
    </ListBox>

希望这一切都是有意义的,我已经尝试了来自某地和其他地方的各种方法,看起来它们应该有效,但并不完全有效!感谢您的帮助。

SelectedItems
返回对象列表,这些对象的字符串表示形式为默认值(即类的名称)。实际上,您可以更改每个选定项的
内容
属性,例如:

string selectAreas = string.Join(", ", lbAreas.SelectedItems.Select(i => i.Content));

lblAreas.Text = selectAreas;
注意:您可能必须将每个项目强制转换为
ListBoxItem

string selectAreas = string.Join(", ", lbAreas.SelectedItems.Cast<ListBoxItem>()
                                                            .Select(i => i.Content));
string selectAreas=string.Join(“,”,lbAreas.SelectedItems.Cast()
.选择(i=>i.Content));

SelectedItems
返回对象列表,这些对象的字符串表示形式是默认值(即类的名称)。实际上,您可以更改每个选定项的
内容
属性,例如:

string selectAreas = string.Join(", ", lbAreas.SelectedItems.Select(i => i.Content));

lblAreas.Text = selectAreas;
注意:您可能必须将每个项目强制转换为
ListBoxItem

string selectAreas = string.Join(", ", lbAreas.SelectedItems.Cast<ListBoxItem>()
                                                            .Select(i => i.Content));
string selectAreas=string.Join(“,”,lbAreas.SelectedItems.Cast()
.选择(i=>i.Content));

太好了,谢谢你的快速回复,戴夫,工作很愉快!如您所述,必须强制转换到ListBoxItem,以供其他阅读者使用。您可以使用文本绑定到listBox。带有转换器的项目将返回连接字符串。非常好,感谢您的快速回复Dave,工作顺利!如您所述,必须强制转换到ListBoxItem,以便其他阅读者阅读。您可以使用文本绑定到ListBoxItems,并使用将返回联接字符串的转换器。