[英]How to programmatically set IsChecked property on ListboxItem?
I have a listbox, which I fill with a list via ItemsSource = list. 我有一个列表框,我通过ItemsSource = list填充列表。
Now, I have another list. 现在,我有另一个清单。 And what I want to do is loop through the ListBox, to see if the ListBoxItem.Name is the same. 我想要做的是遍历ListBox,看看ListBoxItem.Name是否相同。 If so, then the ListBoxItem should be selected. 如果是,则应选择ListBoxItem。
My idea: 我的点子:
List<string> firstList = new List<string>();
List<string> secondList = new List<string>();
Listboxx.ItemsSource = firstList;
foreach (string striing in secondList)
{
foreach (ListBoxItem iitem in Listboxx)
{
if (striing == iitem.Name)
{
iitem.IsSelected = true;
}
}
}
Or is there a way in the ListboxItemTemplate to set the IsChecked bool to {Binding IsCheckedOrNot}? 或者ListboxItemTemplate中有没有办法将IsChecked bool设置为{Binding IsCheckedOrNot}?
Nested loops generally should be avoided when possible. 一般情况下应尽可能避免使用嵌套循环。 Why not do something like this? 为什么不做这样的事情?
foreach(var iitem in Listboxx.Items.Where(i => secondList.Contains(i.Name)))
{
iitem.IsSelected = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.