I have a data grid which I binded to a ObservableCollection object. On an event, I clear the ObservableCollection and add new items to it. When finished, I try to update the DataGrid, but it still shows the old rows. What am I doing wrong? This is my XAML:
<DataGrid
ItemsSource="{Binding }"
AutoGenerateColumns="False"
Name="dgvCurrentFaults"
TabIndex="0"
Background="Transparent"
RowBackground="#B4CDCD"
Foreground="#314E54" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Icon" Width="70" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Icon}" Width="20" Height="20"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridHyperlinkColumn Header="Display" Binding="{Binding Display}" ContentBinding="{Binding Display}" IsReadOnly="True">
<DataGridHyperlinkColumn.ElementStyle>
<Style>
<EventSetter Event="Hyperlink.Click" Handler="dgvCurrentFaults_CellContentClick"/>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
<DataGridTextColumn Header="Fault Name" Binding="{Binding Falut_Name}" Width="150" IsReadOnly="True">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Description" Binding="{Binding Fault_Description}" Width="240" IsReadOnly="True">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Action Required" Binding="{Binding ActionRequired}" Width="200" IsReadOnly="True">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="ID Fault" Binding="{Binding IDFault}" Visibility="Hidden" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
and this is my code
public ObservableCollection<FaultsInfo> infoFaultList { get; set; }
private void UpdateTable()
{
infoFaultList.Clear();
infoFaultList.Add(new infoFault(1));
infoFaultList.Add(new infoFault(2));
dgvCurrentFaults.ItemsSource = null;
dgvCurrentFaults.ItemsSource = infoFaultList;
dgvCurrentFaults.UpdateLayout();
dgvCurrentFaults.Items.Refresh();
}
Edit:
After having many more looks on the subject, I see that the first time I update the DataGrid is on the Loaded event of the UserControl. In this case, the DataGrid update fine. Later, the DataGrid updates on an event that is launched by some communication. In that case, it dose not udate. I thought that maybe the problem is that I try to update it from another thread, although I use Invoke.