我正在使用ICollectionView
绑定wpfdatagrid
。源是可观察的集合。我更新可观察的收集5秒一次。但收集已修改,但未更新至DataGrid
。ICollectionView在UI中未更新
public ICollectionView CollectionView { get; set; }
public ObservableCollection<AgentListEntryViewModel> AgentsViewModel
{
get { return _agentsViewModel; }
set { _agentsViewModel = value; }
}
我将值设置为下面的集合视图。
this.CollectionView = new ListCollectionView(this.AgentsViewModel);
this.CollectionView.Filter = this.SearchAgents;
in xaml
,我将可观察集合绑定到数据网格。
我已在类型的ObservableCollection即实现INotifyPropertyChanged
,在AgentListEntryViewModel
private void LoadAgentComponents(List<AgentStateInfo> agentStateInfos = null)
{
foreach (AgentStateInfo agentStateInfo in agentStateInfos)
{
AgentListEntryViewModel agentEntry = this.AgentsViewModel.SingleOrDefault(agent => agent.AgentStateInfo.AgentId == agentStateInfo.AgentId);
if (agentEntry != null)
{
agentEntry.UpdateAgentEntry(agentStateInfo);
}
}
this.OnPropertyChanged("AgentsViewModel");
}
上述方法我使用来更新观察集合。
<DataGrid VerticalAlignment="Stretch" Grid.Row="2" Grid.ColumnSpan="3" Background="{StaticResource ControlLightColor}" BorderBrush="LightGray" BorderThickness="2" HorizontalAlignment="Stretch"
IsReadOnly="True"
GridLinesVisibility="Vertical"
VerticalGridLinesBrush="LightGray"
CanUserAddRows="False"
CanUserResizeRows="False"
SelectionMode="Single"
AutoGenerateColumns="False"
HeadersVisibility="Column"
SnapsToDevicePixels="True"
VerticalContentAlignment="Center"
ItemsSource="{Binding AgentsViewModel}"
SelectedItem="{Binding SelectedAgentComponent}">
</DataGrid>
上面我用来绑定到DataGrid的xaml。
但datagrid未更新。谁能帮我 。
您需要在setter中调用NotifyOfPropertyChanged。 –
通过添加更新集合或整个集合被更改? – Sankarann