2014-01-24 59 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}"/> 

那麼現在你得到了如何動態更新數據的想法。 希望它有助於....!