屏幕上未反映更新的属性

时间:2013-01-10 10:12:58

标签: c# wpf datagrid

我有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属性中项目的字段。屏幕最终会更新,但只有在取消选择行然后重新选择后才会更新,有时甚至只有在几秒钟后才会更新。

有什么想法吗?如果您需要任何进一步的信息或代码,请随时询问。

1 个答案:

答案 0 :(得分:0)

我不确定这是否有任何帮助,但似乎使用DataTables + DataRows过去可能会引起其他人的问题。

Have a look here

and here