2013-12-23 3 views
0

ItemsSource = list를 통해 목록으로 채울 목록 상자가 있습니다.프로그래밍 방식으로 ListBoxItem에 IsChecked 속성을 설정하는 방법?

이제 다른 목록이 있습니다. ListBoxItem.Name이 같은지 확인하기 위해 ListBox를 반복합니다. 그렇다면 ListBoxItem을 선택해야합니다.

내 생각 :

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; 
     } 
    } 
} 

또는 {IsCheckedOrNot 바인딩}로의 IsChecked의 부울을 설정하는 ListboxItemTemplate있는 방법이?

답변

2

가능한 경우 중첩 루프는 피해야합니다. 왜 이런 식으로하지 않니?

foreach(var iitem in Listboxx.Items.Where(i => secondList.Contains(i.Name))) 
{ 
    iitem.IsSelected = true; 
} 
관련 문제