我是Silverlight MVVM的新手,我正在创建一个项目,我将数据绑定到DataGrid。
这是我的数据库结构:
tblAuthorizationVarification (AuthorizationVarificationid,AuthorizationRequestid,number)
tblAuthorizationRequest(AuthorizationRequestid,name)
tblAuthorizationVarificationDetails(DetailId,AuthorizationRequestid,Amount)
我想在gridview中显示所有授权的总金额。
以下是我的代码,在ViewModel
课程中,我从tblAuthorizationRequest
获得tblAuthorizationVarification
:
PagedCollectionView _AuthorizationVarificationList;
public PagedCollectionView AuthorizationVarificationList
{
get { return _AuthorizationVarificationList; }
set
{
_AuthorizationVarificationList = value;
OnPropertyChanged("AuthorizationVarificationList"); }
}
private void GetVarifications()
{
IsBusy = true;
LoadOperation<AuthorizationVerification> loadOp =
objContext.Load(objContext.GetCreditNotesQuery());
loadOp.Completed += (sender, e) =>
{
IEnumerable<AuthorizationVerification> op =
((LoadOperation<AuthorizationVerification>)sender).Entities;
PagedCollectionView view = new PagedCollectionView(op);
this.AuthorizationVarificationList = view;
cnt = cnt - 1;
if (cnt <= 0)
IsBusy = false;
};
}
AuthorizationVarificationList在Gridview中绑定就像
一样<sdk:DataGrid x:Name="grdCreditNotes"
ItemsSource="{Binding Path=AuthorizationVarificationList}"
SelectedItem="{Binding Path=SelectedCreditNote, Mode=TwoWay}"
AutoGenerateColumns="False" IsReadOnly="True" Grid.Row="2"
VerticalAlignment="Stretch" Margin="0,0,0,0">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Credit No"
Binding="{Binding Path=AuthorizationVerificationId}" Width="200"/>
<sdk:DataGridTextColumn Header="Amount"
Binding="{Binding Path=AuthorizationRequest.Amount}" MinWidth="100"
Width="*"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
对于这个网格领域的特定授权量的显示和,能做什么?
答案 0 :(得分:1)
正如我已经建议的那样,您可以为集合项创建一个视图模型类,并按原样填充它。
public class VerificationViewModel
{
public int AuthorizationVerificationId { get; set; }
public double Amount { get; set; }
}
然后使用LINQ查询对集合进行分组并返回汇总项:
loadOp.Completed += (sender, e) =>
{
IEnumerable<AuthorizationVerification> op =
((LoadOperation<AuthorizationVerification>)sender).Entities;
var models = op.GroupBy(item => item.AuthorizationVerificationId)
.Select(g => new VerificationViewModel
{
AuthorizationVerificationId = g.Key,
Amount = g.Sum(gi => gi.Amount)
})
.ToList();
PagedCollectionView view = new PagedCollectionView(models);
// Everything else is the same
}
//Also change the type of the property which is bound to SelectedItem
public VerificationViewModel SelectedCreditNote { get; set; }
并更改第二列的绑定路径:
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Credit No"
Binding="{Binding Path=AuthorizationVerificationId}" Width="200"/>
<sdk:DataGridTextColumn Header="Amount"
Binding="{Binding Path=Amount}" MinWidth="100" Width="*"/>
</sdk:DataGrid.Columns>
此代码应计算每个Id的金额总和。如果您想要其他聚合,可以更改linq查询。
答案 1 :(得分:0)
如果我理解正确你是在摘要行之后?如果是,请查看以下链接http://leeontech.wordpress.com/2010/02/01/summary-row-in-datagrid/。您可能需要更好地塑造ViewModel后面的Model对象(将模型(在这种情况下为DTO)直接显示给View是很好的做法。)