gpt4 book ai didi

WPF ICollectionView 过滤

转载 作者:行者123 更新时间:2023-12-03 05:18:29 28 4
gpt4 key购买 nike

我已经编写了用于在 ComboBox 中过滤项目的代码:

我的问题是,你会怎么做?

我认为这个带有反射的解决方案可能会非常..

ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
view.Filter += this.FilterPredicate;


private bool FilterPredicate(object value)
{
if (value == null)
return false;

if (String.IsNullOrEmpty(SearchedText))
return true;

int index = value.ToString().IndexOf(
SearchedText,
0,
StringComparison.InvariantCultureIgnoreCase);

if ( index > -1) return true;

return FindInProperties(new string[] { "Property1", "Property2" }, value, SearchedText);
}

private bool FindInProperties(string[] properties, object value, string txtToFind)
{
PropertyInfo info = null;
for (int i = 0; i < properties.Length; i++)
{
info = value.GetType().GetProperty(properties[i]);
if (info == null) continue;

object s = info.GetValue(value, null);
if (s == null) continue;

int index = s.ToString().IndexOf(
txtToFind,
0,
StringComparison.InvariantCultureIgnoreCase);

if (index > -1) return true;
}
return false;
}

最佳答案

为什么不只是这个:

ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
IEqualityComparer<String> comparer = StringComparer.InvariantCultureIgnoreCase;
view.Filter = o => {
Person p = o as Person;
return p.FirstName.Contains(SearchedText, comparer)
|| p.LastName.Contains(SearchedText, comparer);
}

您需要动态搜索属性吗?

关于WPF ICollectionView 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2140930/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com