using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Test.ListView { /// /// WinListView.xaml 的交互逻辑 /// public partial class WinListView : Window { public WinListView() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { var items = List.Items; var view = CollectionViewSource.GetDefaultView(items); view.Filter = Filter; } public bool Filter(Object o) { var display = GetDisplay(o); return true; } /// /// 定义私有依赖属性,用于承载显示值 /// private static readonly DependencyProperty m_ItemDisplayProperty = DependencyProperty.Register("m_ItemDisplay", typeof(object), typeof(WinListView)); /// /// 获取Item的显示值 /// protected object GetDisplay(object item) { var propertyPath ="Content"; if (string.IsNullOrEmpty(propertyPath)) return item?.ToString(); BindingOperations.SetBinding(this, m_ItemDisplayProperty, new Binding(propertyPath) { Source = item }); var propertyValue = GetValue(m_ItemDisplayProperty); BindingOperations.ClearBinding(this, m_ItemDisplayProperty); return propertyValue; } } }