更改ItemsSource后,DataGrid不会更新

编程入门 行业动态 更新时间:2024-10-11 11:21:05
本文介绍了更改ItemsSource后,DataGrid不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个数据网格,该网格已绑定到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不会更新

本文发布于:2023-11-13 11:07:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ItemsSource   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!