我可以将查询中的数据绑定到我的datagrid
,但是当我想格式化datagrid
时遇到问题,如果不清楚,请原谅我:
我有一个类来保存值列表,所以我不需要从C# Anonymous Type
进行转换以使用查询
public class SoftwareSummaryModel
{
public string Software { get; set; }
public int NumberOfUser { get; set; }
}
初始化程序时,我想将默认数据加载到dgSummary
public MainWindow()
{
InitializeComponent();
using (var context = new QuotingEntities(BuildConnectionString()))
{
var query = from p in context.SoftwareTrackings
group p by p.Software into g
select new SoftwareSummaryModel
{
Software = g.Key,
NumberOfUser = g.Count()
};
dgSummary.ItemsSource = query.ToList();
}
}
然后很好,datagrid dgSummary
显示2列Software
和NumberOfUser
。现在,我想修复2列的宽度(我会做一些格式化):
<DataGrid Name="dgSummary" AutoGenerateColumns="True" Grid.Row="2" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Width="160" Binding="{Binding Software}" />
<DataGridTextColumn Width="160" Binding="{Binding NumberOfUser}" />
</DataGrid.Columns>
</DataGrid>
dgSummary
显示2个格式化列和2个无格式列Software
和NumberOfUser
~~~~&gt;总共4列,而我预计只有2列。