【发布时间】:2014-05-09 22:21:30
【问题描述】:
我有一个只显示类的某些属性的 DataGrid。
我想做的是,按列顺序获取绑定属性的列表,我该如何实现?
还有一件事,我只想要可见的列。
[编辑]
以下是我用来在类属性中搜索文本的内容,目前它搜索所有属性。
这将更改为仅搜索 DataGrid 上可见的属性
public static class FullTextSearch<T>
{
public static bool Match(T item, string searchTerm)
{
bool match = _properties.Select(prop => prop(item)).Any(value => value != null && value.ToLower().Contains(searchTerm.ToLower()));
return match;
}
private static List<Func<T, string>> _properties;
public static void FullTextSearchInit()
{
_properties = GetPropertyFunctions().ToList();
}
public static IEnumerable<Func<T, string>> GetPropertyFunctions()
{
var stringProperties = GetStringPropertyFunctions();
var intProperties = GetIntPropertyFunctions();
var decimalProperties = GetDecimalPropertyFunctions();
var dateTimeProperties = GetDateTimePropertyFunctions();
return stringProperties.Concat(decimalProperties).Concat(intProperties).Concat(dateTimeProperties);
}
public static IEnumerable<Func<T, string>> GetStringPropertyFunctions()
{
var propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty)
.Where(p => p.PropertyType == typeof(string)).ToList();
var properties = propertyInfos.Select(GetStringPropertyFunc);
return properties;
}
public static Func<T, string> GetStringPropertyFunc(PropertyInfo propInfo)
{
ParameterExpression x = System.Linq.Expressions.Expression.Parameter(typeof(T), "x");
Expression<Func<T, string>> expression = System.Linq.Expressions.Expression.Lambda<Func<T, string>>(System.Linq.Expressions.Expression.Property(x, propInfo), x);
Func<T, string> propertyAccessor = expression.Compile();
return propertyAccessor;
}
public static IEnumerable<Func<T, string>> GetIntPropertyFunctions()
{
var propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty)
.Where(p => p.PropertyType == typeof(int)).ToList();
var properties = propertyInfos.Select(GetIntPropertyFunc);
return properties;
}
public static Func<T, string> GetIntPropertyFunc(PropertyInfo propInfo)
{
ParameterExpression x = System.Linq.Expressions.Expression.Parameter(typeof(T), "x");
Expression<Func<T, int>> expression = System.Linq.Expressions.Expression.Lambda<Func<T, int>>(System.Linq.Expressions.Expression.Property(x, propInfo), x);
Func<T, int> propertyAccessor = expression.Compile();
return (T item) => propertyAccessor(item).ToString();
}
public static IEnumerable<Func<T, string>> GetDecimalPropertyFunctions()
{
var propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty)
.Where(p => p.PropertyType == typeof(decimal)).ToList();
var properties = propertyInfos.Select(GetDecimalPropertyFunc);
return properties;
}
public static Func<T, string> GetDecimalPropertyFunc(PropertyInfo propInfo)
{
ParameterExpression x = System.Linq.Expressions.Expression.Parameter(typeof(T), "x");
Expression<Func<T, decimal>> expression = System.Linq.Expressions.Expression.Lambda<Func<T, decimal>>(System.Linq.Expressions.Expression.Property(x, propInfo), x);
Func<T, decimal> propertyAccessor = expression.Compile();
return (T item) => propertyAccessor(item).ToString();
}
public static IEnumerable<Func<T, string>> GetDateTimePropertyFunctions()
{
var propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty)
.Where(p => p.PropertyType == typeof(DateTime)).ToList();
var properties = propertyInfos.Select(GetDateTimePropertyFunc);
return properties;
}
public static Func<T, string> GetDateTimePropertyFunc(PropertyInfo propInfo)
{
ParameterExpression x = System.Linq.Expressions.Expression.Parameter(typeof(T), "x");
Expression<Func<T, DateTime>> expression = System.Linq.Expressions.Expression.Lambda<Func<T, DateTime>>(System.Linq.Expressions.Expression.Property(x, propInfo), x);
Func<T, DateTime> propertyAccessor = expression.Compile();
return (T item) => propertyAccessor(item).ToString();
}
}
下面的代码是我如何称呼它的,它找到所有与搜索值匹配的属性并将项目添加到列表中,该列表被处理并添加到 DataGrid 上的 SelectedItems 属性中。
从长远来看,我想让这个通用性足以在任何 DataGrid 上使用。因此,不是搜索所有属性,而是需要更具体,如果我想提供下一个和上一个搜索功能,要转到下一个或上一个单元格匹配,我需要按照与 DataGrid 列匹配的顺序获取这些属性。
最终我想在调用时删除硬编码的类名:
FullTextSearch
和
FullTextSearch
但这将是另一个问题
string sv = SearchValue;
this.Target.SelectedItems.Clear();
var itemsSource = this.Target.Items as IEnumerable;
List<Object> tempItems = new List<Object>();
FullTextSearch<UserViewModel>.FullTextSearchInit();
if (itemsSource != null)
{
foreach (var item in itemsSource)
{
if (FullTextSearch<UserViewModel>.Match((UserViewModel)item, sv))
{
tempItems.Add(item);
}
}
if (tempItems.Count > 0)
{
//Add found items to SelectedItems
}
}
【问题讨论】:
-
不确定您的意思...您想获取绑定到 DataGrid 的属性的名称吗?
-
是什么阻止您访问包含上述属性的类?
-
我知道如何将属性从类中取出,但是我不希望所有属性都从类中取出,只希望在 DataGrid 上可见。
-
数据网格有一个 Columns 集合。那些应该是可见的列。编辑:我猜你想看看每一列的 Visibility 属性。
标签: c# wpf wpfdatagrid