【发布时间】:2015-05-29 12:55:59
【问题描述】:
我正在使用 Windows Phone 应用程序,我想在已经存在的列表中添加项目我的问题是,当我添加第一个项目时它可以,但是当我添加第二个项目时,列表将替换我之前添加的项目
private void btn_profession_Loaded(object sender, RoutedEventArgs e)
{
try
{
selectedKeywordsIds = App.skillIds; // for selected keyword
selectedProfName = App.professionalName;
selectedProfId = App.professionalId;
this.btn_profession.Content = selectedProfName;
Key = App.skillKeywords ;
Pro = App.professionalName;
final1=Key.Replace(Pro, "");
List<string> numbers = final1.Split(',').ToList<string>();
numbers.Add(selectedKeywordsIds);
numbers = numbers.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
listBox1.ItemsSource = null;
listBox1.ItemsSource = numbers;
}
【问题讨论】:
-
嗯,您使用的是 distinct,这意味着每个项目只会出现一次。
-
所以在这里你只调用一次 add ......如果新添加的项目与列表中已经存在的项目相同,则“Distinct()”是一个问题。如果您第二次调用 add 在其他地方显示该位置。
-
先生,当我添加第二个项目时,它将移至下一个索引
-
啊,所以您的意思是在控件中显示时,新项目会替换先前添加的项目?也许你应该选择 numbers.Insert(0, newitem);那么你不是在集合的末尾追加而是在集合的开头?或者对集合或控件使用一些排序进行显示?
标签: c# windows-phone-8