我有Datagrid
如下
<DataGridTextColumn Header="Amount" CellStyle="{StaticResource RightAlignedCellStyle}" Binding="{Binding AmountToAllocate, UpdateSourceTrigger=LostFocus, StringFormat='{}{0:#,0.00}'}" />
<DataGridTemplateColumn Header="Comment" Width="*" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Comment}" TextWrapping="WrapWithOverflow"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Add This Allocation" Command="ac:PICommands.AddAllocation" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
您会看到网格的ItemSource
是属性RenewalRows
,其为ObservableCollection
,如下所示: -
public ObservableCollection<Data.Payment.UnitRenewalsRow> RenewalRows
{
get
{
return renewalRows;
}
set
{
renewalRows = value;
OnPropertyChanged("RenewalRows");
}
}
SelectedItem绑定到SelectedRenewalRow
属性,如下所示: -
public Data.Payment.UnitRenewalsRow SelectedRenewalRow
{
get
{
return renewalsRow;
}
set
{
renewalsRow = value;
//FullAllocationAmount();
OnPropertyChanged("SelectedRenewalRow");
}
}
最后一列中有一个调用命令的按钮。背后的代码如下: -
private void Allocate(object sender, ExecutedRoutedEventArgs e)
{
ap.AddAnAllocation();
}
ap是StaticResource
中引用的DataGrid ItemsSource="{Binding Source={StaticResource AllocatePaymentClass}, Path=RenewalRows}”
类
代码如下: -
public void AddAnAllocation()
{
SelectedRenewalRow.Outstanding = SelectedRenewalRow.Outstanding + SelectedRenewalRow.AmountToAllocate;
Allocation allocation = new Allocation();
allocation.Amount = SelectedRenewalRow.AmountToAllocate;
allocation.PaymentInfo = Payment;
allocation.RenewalInfo = SelectedRenewalRow;
allocation.Propref = PropRef;
allocation.FullAddress = FullAddress;
Allocations.Add(allocation);
Payment.Allocations = Allocations;
//reset
SelectedRenewalRow.AmountToAllocate = 0;
}
我的问题是最后一行。用户点击调用AddAnAllocation()
的按钮后,我希望屏幕立即更新DataGrid
中选定的行,AmountToAllocate
属性显示为零。该属性是上面显示的RenewalRows
属性中项目的字段。屏幕最终会更新,但只有在取消选择行然后重新选择后才会更新,有时甚至只有在几秒钟后才会更新。
有什么想法吗?如果您需要任何进一步的信息或代码,请随时询问。