0

たくさんのアイテムが入ったリストボックスがあります。ユーザーが探しているものを入力できるように検索バーを作成しようとしています。リストボックスには、ユーザーがテキストボックスに入力した内容に関連する項目のみが表示されます。wp7は初めてです

4

1 に答える 1

0

まずMVVM、適切なLayout更新に使用する必要があります。次に、追加のプロパティを使用して、にバインドしTextBoxます。プロパティが更新されたとき-更新する別のプロパティ変更イベントを発生させますListBoxFirePropertyChanged("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 に答える