たくさんのアイテムが入ったリストボックスがあります。ユーザーが探しているものを入力できるように検索バーを作成しようとしています。リストボックスには、ユーザーがテキストボックスに入力した内容に関連する項目のみが表示されます。wp7は初めてです
1 に答える
0
まずMVVM
、適切なLayout
更新に使用する必要があります。次に、追加のプロパティを使用して、にバインドしTextBox
ます。プロパティが更新されたとき-更新する別のプロパティ変更イベントを発生させますListBox
(FirePropertyChanged("FooList");
スニペット内)。
public class Foo
{
public string Name { get; set; }
}
でViewModel
:
public string SearchCriteria
{
get
{
return searchCriteria;
}
set
{
serchCriteria = value;
RaisePropertyChanged("SearchCriteria");
RaisePropertyChanged("FooList");
}
}
private List<Foo> fooList;
public List<Foo> FooList
{
get
{
return fooList.Where(x => x.Name.Contains(searchCriteria));
}
}
でXaml
:
<TextBox x:Name="searchText" Text={Binding SearchCriteria, Mode=TwoWay} />
<ListBox x:Name="elementsList" ItemsSource={Binding FooList, Mode=TwoWay}>
<ListBox.ItemTemplate>
...
</ListBox.ItemTemplate>
</ListBox>
MVVM
パターン
をよりよく理解するためのいくつかのヘルプリンク:
于 2012-07-22T09:20:26.303 に答える