当前位置: 移动技术网 > IT编程>开发语言>.net > ComboBox过滤

ComboBox过滤

2019年07月09日  | 移动技术网IT编程  | 我要评论

厕奴日志,怪探德克,风烟歌

在view中完成数据筛选,无需改变数据源的内容,这样就不必担心在其它地方也使用这个数据源。

从路由事件 textboxbase.textchanged 中获取输入的文本,并设置视图的过滤器就可以了。

collectionviewsource.getdefaultview 方法是返回一个 icollectionview 对象,它是给定源集合的默认视图,然后设置视图的filter属性。

官方文档:

完整示例在我的github

        <combobox width="300" horizontalalignment="center" verticalalignment="center" 
                  itemssource="{binding democollection, relativesource={relativesource findancestor, ancestortype={x:type local:mainwindow}}}" 
                  iseditable="true" istextsearchenabled="false"
                  textboxbase.textchanged="combobox_textchanged"/>
        private void combobox_textchanged(object sender, textchangedeventargs e)
        {
            if (sender is combobox combobox)
            {
                var view = (collectionview)collectionviewsource.getdefaultview(combobox.itemssource);
                view.filter = f => f is string s && s.contains(combobox.text);
            }
        }

demo运行效果图



如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网