我有一个数据网格,该网格已绑定到ObservableCollection对象.在事件上,我清除了ObservableCollection并向其中添加了新项目.完成后,我尝试更新DataGrid,但它仍然显示旧行.我究竟做错了什么?这是我的XAML:
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>这是我的代码
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(); }在对该主题进行了更多研究之后,我看到我第一次更新DataGrid是在UserControl的Loaded事件上.在这种情况下,DataGrid可以更新.稍后,DataGrid将更新由某些通信启动的事件. 在这种情况下,它不是ududate.我以为也许是问题,尽管我使用了Invoke,但我还是尝试从另一个线程更新它.
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.
推荐答案
您应该通知收藏集已更改.
You should be notifying the collection has changed.
您已将datacontext设置为该类,
That class you have set the datacontext to,
需要更改完整属性.
当您替换新的ItemsSource时,对属性进行更改.
When you substitute the new ItemsSource raise propertychanged on the property.
看到这个:
msdn.microsoft/en-us/library/ms743695(v = vs.110).aspx
public ObservableCollection<FaultsInfo> infoFaultList { get { return name; } set { name = value; // Call OnPropertyChanged whenever the property is updated OnPropertyChanged("FaultsInfo"); } }然后,当您像这样进行梳理时:
Then when you do soething like:
infoFaultList = somenewobservablecollectionyoujustfilled;它将通知UI绑定的内容已更改.
It will notify the UI the stuff it's bound to has changed.
更多推荐
更改ItemsSource后,DataGrid不会更新
发布评论