在Xamarin表单中的ListView中实现OnSearchBarButtonPressed

时间:2014-10-10 20:21:00

标签: xamarin xamarin.forms

有没有办法在Xamarin表格中搜索表格(Listview)而不使用任何MonoTouch组件 - 用于跨平台?

我有很多名字,并在listview上填充,我想要的是,一旦用户输入名称,列表视图将被排序,它应该带来匹配的名称。我在listview的顶部添加了搜索栏。

我不知道如何实现OnSearchBarButtonPressed方法。我相信我不应该再重新发明轮子。

           SearchBar searchBar = new SearchBar
            {
                Placeholder = "Search Employee Name",
            };
            searchBar.SearchButtonPressed += OnSearchBarButtonPressed;

            Padding = new Thickness (10, 20, 10, 10);
            Content = new StackLayout () {
                Children={searchBar,listView}

            };

1 个答案:

答案 0 :(得分:2)

与您的其他帖子here相关, ListView ItemsSource 是一组对象。

ListView 控件无法了解您的模型无法自行进行任何过滤。< / p>

ListView 中似乎没有任何内容可以让您指定一些谓词来帮助完成此任务。

因此,您应该在 SearchBar.SearchButtonPressed 中进行过滤,然后重新分配 ListView ItemsSource 以反映新过滤的 ItemsSource 您要向最终用户展示。您的过滤逻辑可以是您想要的任何东西,因为您可以自定义它甚至可以过滤许多字段等。

SearchBar.SearchButtonPressed 只是一个事件,您可以像指定的那样分配 EventHandler 。您只需填写过滤逻辑并相应地更新 ListView