我有一个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&LT; UserViewModel&GT; .FullTextSearchInit();
和
FullTextSearch&LT; UserViewModel&GT; .Match()
但这将是另一个问题
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
}
}
答案 0 :(得分:2)
我不确定这是否有帮助:
var visibleColumns = grid.Columns.Where(c => c.Visibility == System.Windows.Visibility.Visible).ToList();