作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的数据网格的项目源是一组对象,例如:
Public Property Quarter As Integer
Public Property MyColumns() As New List(Of MyColumn)
现在我想要一个网格绑定(bind),使我的结果网格看起来像
-- Quarter -- Column1 -- Column2 -- Column3 .... ColumnX
数据源中的所有项目都将具有相同的 MyColumns。
有没有一种方法可以将集合绑定(bind)到网格列?
最佳答案
这是解决方案。它不是最漂亮或最简单的,但它是可配置的。它主要基于帖子 WPF: Dictionary<int, List<string>> in DataGrid 中的想法。 ,只是变成了一个更通用的版本,带有一些表达式和反射。
无论用作 ItemsSource
的项目在各自的集合属性中包含相同数量的项目还是不同数量的项目,它都会起作用。
以下是必需的组件:
using System.Reflection;
using Expressions = System.Linq.Expressions;
// See - https://stackoverflow.com/questions/2132791/reflecting-over-all-properties-of-an-interface-including-inherited-ones
public static class ReflectionExtensions
{
public static PropertyInfo GetInterfaceProperty(this Type type, String propName, Type returnType)
{
if (propName == null)
throw new ArgumentNullException("propName");
if (returnType == null)
throw new ArgumentNullException("propType");
return type.GetInterfaces()
.Select(parentInterface =>
parentInterface.GetProperty(propName, returnType))
.Where(prop =>
prop != null)
.Single();
}
}
public static class CollectionPropertyDataGridBindingHelper
{
public static void RemoveAutoGeneratedColumns(this DataGrid dataGrid, String propertyName)
{
if (dataGrid == null)
throw new ArgumentNullException("dataGrid");
if (propertyName == null)
throw new ArgumentNullException("propertyName");
var autogeneratedColumns = dataGrid
.Columns
.OfType<DataGridBoundColumn>()
.Where(col =>
(col.Binding as Binding).Path.Path.Equals(propertyName));
foreach (var autoColumn in autogeneratedColumns)
{
dataGrid.Columns.Remove(autoColumn);
}
}
public static void RegenerateColumns<TItem, TPropertyCollectionItem>(
this DataGrid dataGrid,
Expressions.Expression<Func<TItem, IEnumerable<TPropertyCollectionItem>>> propertyExpression,
IEnumerable<TItem> items)
{
RegenerateColumns<TItem, TPropertyCollectionItem>(dataGrid,
propertyExpression,
items,
(index) =>
String.Format("Column - {0}", index));
}
public static void RegenerateColumns<TItem, TPropertyCollectionItem>(
this DataGrid dataGrid,
Expressions.Expression<Func<TItem, IEnumerable<TPropertyCollectionItem>>> collectionPropertyExpression,
IEnumerable<TItem> items,
Func<Int32, String> formatHeader)
{
if (dataGrid == null)
throw new ArgumentNullException("dataGrid");
if (collectionPropertyExpression == null)
throw new ArgumentNullException("propertyExpression");
if (items == null)
throw new ArgumentNullException("items");
if (formatHeader == null)
throw new ArgumentNullException("formatHeader");
var collectionPropInfo = GetCollectionPropertyInfoFor<TItem, TPropertyCollectionItem>(collectionPropertyExpression);
var propertyName = collectionPropInfo.Name;
var getCount = GetCountGetter<TItem, TPropertyCollectionItem>(
collectionPropertyExpression.Compile(),
collectionPropInfo);
// Remove old autocolumns
dataGrid.RemoveAutoGeneratedColumns(propertyName);
Int32 columnsRequired = items.Select(item => getCount(item)).Max();
// Create new columns
GenerateColumns(dataGrid,
formatHeader,
propertyName,
columnsRequired);
}
private static void GenerateColumns(DataGrid dataGrid,
Func<Int32, String> formatHeader,
String propertyName,
Int32 columnsRequired)
{
for (int columnNumber = 0; columnNumber < columnsRequired; columnNumber++)
{
DataGridTextColumn column = new DataGridTextColumn()
{
Header = formatHeader(columnNumber),
Binding = new Binding(String.Format("{0}[{1}]",
propertyName,
columnNumber))
};
dataGrid.Columns.Add(column);
}
}
private static Func<TItem, Int32> GetCountGetter<TItem, TPropertyCollectionItem>(
Func<TItem, IEnumerable<TPropertyCollectionItem>> getCollection,
PropertyInfo propInfo)
{
if (getCollection == null)
throw new ArgumentNullException("getCollection");
if (propInfo == null)
throw new ArgumentNullException("propInfo");
var collectionType = propInfo.PropertyType;
var countGetter = collectionType.GetInterfaceProperty("Count",
typeof(Int32));
if (countGetter != null)
{
return (item) =>
(Int32)countGetter.GetMethod.Invoke(getCollection(item), null);
}
throw new NotImplementedException("Not implemented: For simple IEnumerables the use of Enumerable.Count() method shall be considered.");
}
private static PropertyInfo GetCollectionPropertyInfoFor<TItem, TPropertyCollectionItem>(
Expressions.Expression<Func<TItem,
IEnumerable<TPropertyCollectionItem>>> propertyExpression)
{
if (propertyExpression == null)
throw new ArgumentNullException("propertyExpression");
var memberExp = propertyExpression.Body as Expressions.MemberExpression;
if (memberExp == null)
throw new ArgumentNullException("propertyExpression");
var propInfo = memberExp.Member as PropertyInfo;
if (propInfo == null)
throw new ArgumentNullException("propertyExpression");
if (!propInfo.DeclaringType.IsAssignableFrom(typeof(TItem)))
throw new ArgumentException("propertyExpression");
return propInfo;
}
}
这是 XAML:
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" Name="dataGrid">
<DataGrid.Columns>
<DataGridTextColumn Header="Quarter" Binding="{Binding Quarter}"/>
</DataGrid.Columns>
</DataGrid>
这是代码隐藏:
using Expressions = System.Linq.Expressions
public class Item
{
public Item(Int32 quarter, Int32 repeatColumns)
{
this.Quarter = quarter;
this.MyColumns = Enumerable
.Range(1, repeatColumns)
.ToList();
}
public Int32 Quarter
{
get; set;
}
public IList<Int32> MyColumns
{
get; set;
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Items = GetOriginalItems();
this.DataContext = this;
this.ReinitializeColumns();
}
private void ReinitializeColumns()
{
Expressions.Expression<Func<Item, IEnumerable<Int32>>> exp =
obj =>
obj.MyColumns;
this.dataGrid.RegenerateColumns(exp,
this.Items);
}
public IEnumerable<Item> Items
{
get;
private set;
}
public IEnumerable<Item> GetOriginalItems()
{
return new Item[]
{
new Item(1, 3),
new Item(2, 2),
new Item(3, 5),
new Item(4, 2),
};
}
}
这是将创建指定数量的列的代码(你可以把它放到一些独立的类中,因为它是完全独立的,或者放到具有任意长度方法的同一个类中(在这种情况下不要忘记删除重复的生成方法))。它更简单一些,直接适合您的需求:
public static void RegenerateColumns<TItem, TPropertyCollectionItem>(
this DataGrid dataGrid,
String propertyName,
Int32 columnsRequired)
{
dataGrid.RegenerateColumns<TItem, TPropertyCollectionItem>(propertyName,
columnsRequired,
index => String.Format("Column - {0}",
index));
}
public static void RegenerateColumns<TItem, TPropertyCollectionItem>(
this DataGrid dataGrid,
String propertyName,
Int32 columnsRequired,
Func<Int32, String> formatHeader)
{
if (dataGrid == null)
throw new ArgumentNullException("dataGrid");
if (propertyName == null)
throw new ArgumentNullException("propertyName");
if (columnsRequired < 0)
throw new ArgumentOutOfRangeException("columnsRequired");
if (formatHeader == null)
throw new ArgumentNullException("formatHeader");
// Remove old autocolumns
dataGrid.RemoveAutoGeneratedColumns(propertyName);
GenerateColumns(dataGrid,
formatHeader,
propertyName,
columnsRequired);
}
private static void GenerateColumns(DataGrid dataGrid,
Func<Int32, String> formatHeader,
String propertyName,
Int32 columnsRequired)
{
for (int columnNumber = 0; columnNumber < columnsRequired; columnNumber++)
{
DataGridTextColumn column = new DataGridTextColumn()
{
Header = formatHeader(columnNumber),
Binding = new Binding(String.Format("{0}[{1}]",
propertyName,
columnNumber))
};
dataGrid.Columns.Add(column);
}
}
它是使用它的代码隐藏:
public MainWindow()
{
InitializeComponent();
this.Items = GetOriginalItems();
this.DataContext = this;
this.ReinitializeColumns(2);
}
private void ReinitializeColumns(Int32 columnsCount)
{
this.dataGrid.RegenerateColumns<Item, Int32>("MyColumns",
columnsCount);
}
关于c# - 如何为用作 DataGrid 的 ItemsSource 的项目的集合属性中的每个项目生成和绑定(bind)一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29172138/
我是一名优秀的程序员,十分优秀!