更新DataGrid

时间:2014-05-07 17:59:52

标签: c# wpfdatagrid

我有一个包含已用时间列的网格,我希望网格每秒更新一次。

private ObservableCollection<ACEvent> _acEvents
public ObservableCollection<ACEvent> AcEvents
    {
        set
        {
            _acEvents = value;
            PendingEventsGrid.ItemsSource = _acEvents;
            ElapsedTimer.Start();
        }
    }

public UI()
    {
        InitializeComponent();
        ElapsedTimer.Tick += ElapsedTimer_Tick;
        ElapsedTimer.Interval = new TimeSpan(0,0,0,1);
        _acEvents = new ObservableCollection<ACEvent>();
    }

void ElapsedTimer_Tick(object sender, EventArgs e)
    {
        try
        {
            foreach (var acEvent in _acEvents)
            {
                acEvent.Elapsed = DateTime.Now - acEvent.Timestamp;
            }

            PendingEventsGrid.Items.Refresh();
        }
        catch (Exception ex)
        {}            
    }

<DataGrid Name="PendingEventsGrid" IsReadOnly="True" ItemsSource="{Binding}" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="300,136,0,0" VerticalAlignment="Top" Height="174" Width="135">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Elapsed" Binding="{Binding Elpased}"></DataGridTextColumn>
            <DataGridTextColumn Header="Event" Binding="{Binding EventDescription}"></DataGridTextColumn>
        </DataGrid.Columns>    
    </DataGrid>

'Event'列从_acEvents填充,但'Elapsed'列中没有任何内容。

我在这里缺少什么?

0 个答案:

没有答案