12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 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
- {
- /// <summary>
- /// WinListView.xaml 的交互逻辑
- /// </summary>
- 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;
- }
- /// <summary>
- /// 定义私有依赖属性,用于承载显示值
- /// </summary>
- private static readonly DependencyProperty m_ItemDisplayProperty =
- DependencyProperty.Register("m_ItemDisplay", typeof(object), typeof(WinListView));
- /// <summary>
- /// 获取Item的显示值
- /// </summary>
- 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;
- }
- }
- }
|