처음에는 코드 (미안하지만 100 %는 아님)는 전문가가 아니며 질문이 이어집니다. 당신은 지금 이해 코드가 BTW 매력처럼 작동하는 텍스트 상자에 입력을 기반으로 목록 상자를 필터링한다는 것입니다 것 같은 목록 상자 항목 선택 유지
public partial class Window1 : Window
{
CollectionView cv;
public Window1()
{
InitializeComponent();
List<Person> ppl = new List<Person>();
BitmapImage b = new BitmapImage(new Uri(@"http://i.stackoverflow.com/Content/Img/stackoverflow-logo-250.png",UriKind.Absolute));
ppl.Add(new Person(b, "world1"));
ppl.Add(new Person(b, "world2"));
ppl.Add(new Person(b, "world3"));
ppl.Add(new Person(b, "world4"));
ppl.Add(new Person(b, "world5"));
ppl.Add(new Person(b, "world6"));
lb.ItemsSource = ppl;
lb.SelectedIndex = 1;
cv = (CollectionView)CollectionViewSource.GetDefaultView(lb.ItemsSource);
new TextSearchFilter(cv, textBox1);
}
}
public class TextSearchFilter
{
public TextSearchFilter(CollectionView cv, TextBox tb)
{
string filterText = "";
cv.Filter = delegate(object obj)
{
Person p = obj as Person;
int index = p.Info.IndexOf(filterText,0,StringComparison.InvariantCultureIgnoreCase);
return index > -1;
};
tb.TextChanged += delegate
{
filterText = tb.Text;
cv.Refresh();
};
}
}
class Person
{
private BitmapImage myImage;
private string myInfo = "";
public BitmapImage Image
{
get { return myImage; }
set { myImage = value; }
}
public string Info
{
get { return myInfo; }
set { myInfo = value; }
}
public Person(BitmapImage Image, string Info)
{
this.Image = Image;
this.Info = Info;
}
}
는 지금까지 읽어 주셔서 감사합니다.
내 문제는 필터링하는 동안 선택을 유지하는 방법입니다. 창이로드되면 목록 상자에 모든 항목이 들어 있고 첫 번째 항목을 선택한 다음 텍스트 상자에 항목을 입력하고 목록 상자 필터를 사용하여 다른 항목을 선택한 후 관련 항목 만 표시합니다. 텍스트에서 모든 텍스트를 제거합니다. 상자를 원래 상태로 되돌려 놓았지만 이번에는 필터링 된보기에서 선택한 항목으로 만 선택 항목이 변경되었습니다 (두 개의 항목이 선택된 것으로 표시되는 대신 선택한 항목이 하나만 표시됨). 이 동작은 컬렉션에서 필터링 할 때 분명하므로 컬렉션이 변경되면 선택 영역이 손실됩니다.
선택을 유지할 수있는 방법이 있습니까? 어떤 포인터?
많은 감사. 당신의 TextChanged 이벤트 핸들러에서 아마