0

I am using ICollectionView to bind wpfdatagrid.ICollectionView source is observable collection. i update the observable collection 5 seconds once. but the collection is modified, but it is not updating to DataGrid.

public ICollectionView CollectionView { get; set; }

public ObservableCollection<AgentListEntryViewModel> AgentsViewModel
{
     get { return _agentsViewModel; }
     set { _agentsViewModel = value; }
}

i set the value to collection view below.

this.CollectionView = new ListCollectionView(this.AgentsViewModel);
this.CollectionView.Filter = this.SearchAgents;

in xaml, I bind observable collection to data grid.

I have implemented INotifyPropertyChanged in type of ObservableCollection i.e., in 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");
    }

the above method i am using to update the observable collection.

 <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>

the above xaml i used to bind to DataGrid.

but The datagrid not updated. can anyone help me .

Ram Nivas
  • 142
  • 3
  • 13

3 Answers3

2

Ram Nivas,

It appears that you dont have your INotifyPropertyChanged applied to your AgentsViewModel property.. Should read something like

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

Although you may have implemented the INotifyPropertyChanged interface on your AgentListEntryViewModel model you must also apply the same logic to the AgentsViewModel.

The reason for this is the Binder will be listening for the notification against the property AgentsViewModel and not listening for changes on the properties of each AgentListEntryViewModel.

Nico
  • 12,493
  • 5
  • 42
  • 62
1

You are not adding/deleting from collection (so INotifyCollectionChanged does not come into picture).

Instead you are modifying some property in underlying data class (AgentListEntryViewModel) which is evident from this piece of code:

agentEntry.UpdateAgentEntry(agentStateInfo);

If you are updating some property say AgentStateInfo then that property should raise PropertyChangedEvent i.e. AgentListEntryViewModel should implement INPC.

But in case you want to refresh complete view, you can call Refresh() on ICollectionView instance (not good idea though) once you are done with modifying property:

CollectionView.Refresh();
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
0

Use the INotifyPropertyChanged event and inherit your Datacontext class from it

then use the following code in that class

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;
}

and let set your property in an observable collection. So that is it is updated means it will changed in the UI.

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

and in the xaml page declare your control like this

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

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

Well now you got an idea of how to update the data dynamically. Hope it helps....!