2014-01-24 63 views
0

我正在使用ICollectionView绑定wpfdatagrid。源是可观察的集合。我更新可观察的收集5秒一次。但收集已修改,但未更新至DataGridICollectionView在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未更新。谁能帮我 。

+0

您需要在setter中调用NotifyOfPropertyChanged。 –

+2

通过添加更新集合或整个集合被更改? – Sankarann

回答

1

拉姆Nivas,

看来,你没有你的INotifyPropertyChanged应用到您的AgentsViewModel财产..应改为类似

public ObservableCollection<AgentListEntryViewModel> AgentsViewModel 
{ 
    get { return _agentsViewModel; } 
    set 
    { 
      if(_agentsViewModel != value){ 
      _agentsViewModel = value; 
      OnPropertyChanged("AgentsViewModel"); //or however you are calling your property changed 
      } 
    } 
} 

虽然你可能已经实现了您AgentListEntryViewModel模式INotifyPropertyChanged界面你还必须将相同的逻辑应用于AgentsViewModel

其原因是Binder将倾听对房产AgentsViewModel的通知,并且不倾听每个AgentListEntryViewModel属性的更改。

+0

Nico。它不工作。 –

+0

您可以在编辑中发布代码更改吗?还有一个XAML绑定的样本? – Nico

+0

尼科,我编辑的问题,你可以看到那里。 –

0

你是not adding/deleting from collection(所以INotifyCollectionChanged没有进入图片)。

相反,你正在修改中的基础数据类(AgentListEntryViewModel),这是显而易见的,从这段代码的一些特性:

agentEntry.UpdateAgentEntry(agentStateInfo); 

如果要更新一些财产说AgentStateInfo那么财产应提高PropertyChangedEventAgentListEntryViewModel应该实现INPC。

,但如果你要刷新完整视图,您可以拨打ICollectionView实例(尽管不是好主意)Refresh()一旦你与修改属性来完成:

CollectionView.Refresh(); 
0

使用INotifyPropertyChanged事件和继承你的DataContext类从它

然后用下面的代码在类

public event PropertyChangedEventHandler PropertyChanged; 



protected void RaisePropertyChanged<T>(Expression<Func<T>> action) 
{ 
    var propertyName = GetPropertyName(action); 
    RaisePropertyChanged(propertyName); 
} 
private static string GetPropertyName<T>(Expression<Func<T>> action) 
{ 
    var expression = (MemberExpression)action.Body; 
    var propertyName = expression.Member.Name; 
    return propertyName; 
} 

,让您设定prope在可观察的集合中。所以这是更新意味着它会在用户界面中发生变化。

 public string UserName 
     { 
      get 
      { 
       return _userName; 
      } 
      set 
      { 
       if (_userName != value) 
       { 
        _userName = value; 
        RaisePropertyChanged(() => UserName); 
       } 
      } 
     } 

,并在XAML页面声明你控制这样

<TextBlock HorizontalAlignment="Left" FontWeight="Normal" Margin="2" MinWidth="100" 

Grid.Row="6" Grid.Column="2" FontFamily="Calibri" Name="txtKey14" VerticalAlignment="Center" Text="{Binding UserName}"/> 

那么现在你得到了如何动态更新数据的想法。 希望它有助于....!