WinListView.xaml.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. namespace Test.ListView
  15. {
  16. /// <summary>
  17. /// WinListView.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class WinListView : Window
  20. {
  21. public WinListView()
  22. {
  23. InitializeComponent();
  24. }
  25. private void Button_Click(object sender, RoutedEventArgs e)
  26. {
  27. var items = List.Items;
  28. var view = CollectionViewSource.GetDefaultView(items);
  29. view.Filter = Filter;
  30. }
  31. public bool Filter(Object o)
  32. {
  33. var display = GetDisplay(o);
  34. return true;
  35. }
  36. /// <summary>
  37. /// 定义私有依赖属性,用于承载显示值
  38. /// </summary>
  39. private static readonly DependencyProperty m_ItemDisplayProperty =
  40. DependencyProperty.Register("m_ItemDisplay", typeof(object), typeof(WinListView));
  41. /// <summary>
  42. /// 获取Item的显示值
  43. /// </summary>
  44. protected object GetDisplay(object item)
  45. {
  46. var propertyPath ="Content";
  47. if (string.IsNullOrEmpty(propertyPath))
  48. return item?.ToString();
  49. BindingOperations.SetBinding(this, m_ItemDisplayProperty, new Binding(propertyPath) { Source = item });
  50. var propertyValue = GetValue(m_ItemDisplayProperty);
  51. BindingOperations.ClearBinding(this, m_ItemDisplayProperty);
  52. return propertyValue;
  53. }
  54. }
  55. }