.NET 4.0 WPF,带有行和列标题的 DataGrid
我想要一个带有行标题和列标题的 DataGrid。以下是我的数据结构:
public class PointInfo
{
public Tuple<int, int> Coordinate { get; set; }
public int DataAtPoint { get; set; }
private PointInfo(){}
public PointInfo(int rowIndex, int columnIndex)
{
Coordinate = Tuple.Create(rowIndex, columnIndex);
}
}
我想创建一个 DataGrid 并将列标题分配给 columnIndex
并将行标题分配给 rowIndex
。我猜这是可能的。我看过这个问题。它已经非常接近解决问题了。
DataGrid 的数据将来自 PointInfo 对象的列表。它基本上就像一场战斗
感谢!
I'd like to have a DataGrid with row and column headers. Following is my data structure:
public class PointInfo
{
public Tuple<int, int> Coordinate { get; set; }
public int DataAtPoint { get; set; }
private PointInfo(){}
public PointInfo(int rowIndex, int columnIndex)
{
Coordinate = Tuple.Create(rowIndex, columnIndex);
}
}
Id like to create a DataGrid and assign column header to the columnIndex
and assign row header to rowIndex
. Im guessing this is possible. Ive had a look at this question. It comes quite close to solving the issue.
Data for the DataGrid will come from a list of PointInfo
objects. Its basically like a Battle
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是您问题的完整答案,但 WPF DataGrid 不太擅长处理可变数量的列。
您可以做的是动态创建匿名类型(例如 http://jacobcarpenter.wordpress.com/2008/03/13/dictionary-to-anonymous-type/),并打开自动生成列,以便通过反射来实现,否则您将得到在代码中定义您的列。
另请注意,绑定列标题(而不是直接设置它)可能会变得很麻烦,因为 DataGrid 列不会继承父级的 DataContext,但这里提供了一种解决方法: http://blogs.infragistics.com/blogs/josh_smith/archive /2008/06/26/data-binding-the-isvisible-property-of-contextualtabgroup.aspx。
Not a whole answer to your question, but WPF DataGrid isn't great at playing nice with a variable number of columns.
What you might be able to do is create an anonymous type dynamically (e.g., http://jacobcarpenter.wordpress.com/2008/03/13/dictionary-to-anonymous-type/), and turn on auto generate columns so it does so through reflection, otherwise you'll have to define your columns in code.
Note also that binding the column header (as opposed to setting it directly) could get hairy because DataGrid columns don't inherit the parent's DataContext, but there's a workaround presented here: http://blogs.infragistics.com/blogs/josh_smith/archive/2008/06/26/data-binding-the-isvisible-property-of-contextualtabgroup.aspx.