[英]C# WPF ListBox to string
如何将ListBox内容传递给字符串(字符串数组?)。 我需要获取类似的数据格式:
Value1
Value2
Value3
其中每个值是一个单独的ListBox项。
编辑:我对我的问题不太准确。 我想通过WPF应用程序通过电子邮件发送所需的值列表,不幸的是,值在一行中发送。 甚至使用/ n也无济于事。 我该怎么做才能将外观更改为列表而不是字符串?
public string[] get()
{
string[] arr = new string[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
arr[i] = listBox1.Items[i].ToString();
}
return arr;
}
简单的一线解决方案:
List<string> items = ListBox1.Items.Cast<ListBoxItem>().Select(p=> p.Content as string).ToList();
您可以使用单个字符串值而不是数组来存储结果,如下所示:
string emailData=string.Empty;
//get each item's text & append emailData.
foreach(var item in listBox1.Items)
{
string itemData=((ContentControl)item).Content.ToString();
emailData = emailData +itemData+"\n";
}
//Call the method that writes the data to email, remember to use Trim() while using emailData.
WriteToEmail(emailData.Trim());
//lb is the Listbox; array is a string[]
lb.ItemsSource= array;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.