title: refresh datagrid on view from viewmodel
body: <p>I have a datagrid on a view that is bound to a viewmodel. When I initialze the view, the datagrid is populated with data from the viewmodel (ObservableCollection) as it should. However, with I try to do a search against the data, the datagrid does not get refreshed from the viewmodel. When I breakpoint the code in my viewmodel, I can see the results in the ObservableCollection has changed per my search, but somehow that is not getting communicated back to the view. Here is my view and viewmodel (BTW, I am using VS2010 RTM):</p>
  
  <pre><code>namespace Attendance.ViewModels
  {
      public class EmployeeSelectViewModel : ViewModel, INotifyPropertyChanged
      {
          #region Entity list and constructor
      public EmployeeSelectViewModel()
      {
          {
              Initialize();
          }
      }
  
      private void Initialize()
      {
          if (employeeRpository == null)
              employeeRpository = new EmployeeRepository();
  
          ListOfEmployees = new ObservableCollection&lt;EmployeeDto&gt;(employeeRpository.GetEmployees(true));
      }
  
      private EmployeeRepository employeeRpository;
  
      private ObservableCollection&lt;EmployeeDto&gt; listOfEmployees;
      public ObservableCollection&lt;EmployeeDto&gt; ListOfEmployees
      {
          get { return listOfEmployees; }
          set
          {
              if (listOfEmployees != value)
              {
                  listOfEmployees = value;
                  NotifyPropertyChanged("ListOfEmployee");
              }
          }
      }
  
      private EmployeeDto selectedEmployee;
      public EmployeeDto SelectedEmployee
      {
          get { return selectedEmployee; }
          set
          {
              if (selectedEmployee != value)
              {
                  selectedEmployee = value;
                  NotifyPropertyChanged("SelectedEmployee");
              }
          }
      }
  
      #endregion
  
      #region UI control references
  
      /// &lt;summary&gt;
      /// search text property
      /// &lt;/summary&gt;
      private string searchText;
      public string SearchText
      {
          get { return searchText; }
          set
          {
              if (searchText != value)
              {
                  searchText = value;
                  NotifyPropertyChanged("SearchText");
              }
          }
      }
  
      public string Location { get; set; }
  
      #endregion
  
      #region Relay Commands
  
      /// &lt;summary&gt;
      /// new command
      /// &lt;/summary&gt;
      private ViewCommand newCommand;
      public ViewCommand NewCommand
      {
          get
          {
              if (newCommand == null)
                  newCommand = new ViewCommand(param =&gt; this.NewEmployee());
              return newCommand;
          }
      }
      private void NewEmployee()
      {
          NavigationActions.NewEmployeeView();
      }
  
      /// &lt;summary&gt;
      /// edit command
      /// &lt;/summary&gt;
      private ViewCommand editCommand;
      public ViewCommand EditCommand
      {
          get
          {
              if (editCommand == null)
              {
                  editCommand = new ViewCommand(param =&gt; this.EditEmployee());
              }
              return editCommand;
          }
      }
  
      private void EditEmployee()
      {
          NavigationActions.OpenEmployeeView(SelectedEmployee);
      }
  
      /// &lt;summary&gt;
      /// save command
      /// &lt;/summary&gt;
      private ViewCommand saveCommand;
      public ViewCommand SaveCommand
      {
          get
          {
              if (saveCommand == null)
              {
                  saveCommand = new ViewCommand(
                          param =&gt; this.SaveEmployee(),
                          param =&gt; this.CanSaveEmployee
                          );
              }
              return saveCommand;
          }
      }
  
      public void SaveEmployee()
      {
          employeeRpository.SaveChanges();
      }
  
      private bool CanSaveEmployee
      {
          get { return true; }
      }
  
      /// &lt;summary&gt;
      /// clear search command
      /// &lt;/summary&gt;
      private ViewCommand clearSearchCommand;
      public ViewCommand ClearSearchCommand
      {
          get
          {
              if (clearSearchCommand == null)
                  clearSearchCommand = new ViewCommand(param =&gt; this.ClearSearch());
              return clearSearchCommand;
          }
      }
  
      private void ClearSearch()
      {
          this.SearchText = string.Empty;
          ListOfEmployees = new ObservableCollection&lt;EmployeeDto&gt;(employeeRpository.GetEmployees(true));
      }
  
      /// &lt;summary&gt;
      /// search command
      /// &lt;/summary&gt;
      private ViewCommand searchCommand;
      public ViewCommand SearchCommand
      {
          get
          {
              if (searchCommand == null)
                  searchCommand = new ViewCommand(param =&gt; this.SearchEmployee());
              return searchCommand;
          }
      }
  
      private void SearchEmployee()
      {
          if (this.SearchText == string.Empty || this.SearchText == null)
          {
              NavigationActions.ShowError("Search Employees.", "Please enter your search text ...");
              return;
          }
          ListOfEmployees = new ObservableCollection&lt;EmployeeDto&gt;(employeeRpository.GetEmployeesByQuery(SearchText, Location));
      }
  
      /// &lt;summary&gt;
      /// exit command
      /// &lt;/summary&gt;
      private ViewCommand exitCommand;
      public ViewCommand ExitCommand
      {
          get
          {
              if (exitCommand == null)
              {
                  exitCommand = new ViewCommand(param =&gt; this.ExitWindow());
              }
              return exitCommand;
          }
      }
  
      private void ExitWindow()
      {
          NavigationActions.CloseCurrentView();
      }
      #endregion
  
  
      #region INotifyPropertyChanged Members
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      public void NotifyPropertyChanged(String info)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(info));
          }
      }
  
      #endregion
      }
  }
  
  &lt;Window x:Class="Attendance.Views.EmployeeSelectView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:vm="clr-namespace:Attendance.ViewModels"
      Title="Employee Maintenance" FocusManager.FocusedElement="{Binding ElementName=txtSearchCriteria}"
      Height="525" Width="800" WindowStartupLocation="CenterScreen" WindowState="Normal"
      WindowStyle="SingleBorderWindow" Icon="Images/gb_icon.png"&gt;
  &lt;Window.DataContext&gt;
      &lt;vm:EmployeeSelectViewModel /&gt;
  &lt;/Window.DataContext&gt;
  &lt;Window.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary Source="Themes/DataGrid.Generic.xaml" /&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;!--xml data start--&gt;
          &lt;XmlDataProvider x:Key="LocationData" XPath="LocationList/LocationItem" Source="XMLData/Location.xml"/&gt;
          &lt;!--xml data end--&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid Width="775"&gt;
      &lt;DockPanel HorizontalAlignment="Left" Width="770"&gt;
          &lt;!-- TOOLBAR --&gt;
          &lt;DockPanel DockPanel.Dock="Top" MinHeight="30" Margin="5"&gt;
              &lt;ToolBar FontWeight="Bold"&gt;
                  &lt;!-- NEW --&gt;
                  &lt;Button Name="btnNew" Command="{Binding Path=NewCommand}"&gt;
                      &lt;Button.ToolTip&gt;
                          &lt;StackPanel&gt;
                              &lt;Label FontWeight="Bold" Background="SteelBlue" Foreground="White"&gt;
                                  Create a new Customer
                              &lt;/Label&gt;
                              &lt;TextBlock Padding="10" TextWrapping="WrapWithOverflow" Width="200"&gt;
                              Create a new customer in a new Window tab.
                              &lt;/TextBlock&gt;
                              &lt;Line Stroke="SteelBlue" StrokeThickness="1" X2="200" /&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;Image Margin="2" Source="Images/new.png"/&gt;
                                  &lt;Label&gt;Press F1 for more help&lt;/Label&gt;
                              &lt;/StackPanel&gt;
                          &lt;/StackPanel&gt;
                      &lt;/Button.ToolTip&gt;
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;Image Source="Images/new.png" Width="22" Height="22" Margin="2"/&gt;
                          &lt;Label VerticalAlignment="Center"&gt;_New&lt;/Label&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Button&gt;
                  &lt;!-- EDIT --&gt;
                  &lt;Button Name="btnEdit" Command="{Binding Path=EditCommand}"&gt;
                      &lt;Button.ToolTip&gt;
                          &lt;StackPanel&gt;
                              &lt;Label FontWeight="Bold" Background="SteelBlue" Foreground="White"&gt;
                                  Edit the current record
                              &lt;/Label&gt;
                              &lt;TextBlock Padding="10" TextWrapping="WrapWithOverflow" Width="200"&gt;
                              Edit the current selected Customer.
                              &lt;/TextBlock&gt;
                              &lt;Line Stroke="SteelBlue" StrokeThickness="1" X2="200" /&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;Image Margin="2" Source="Images/dialog-information.png"/&gt;
                                  &lt;Label&gt;Press F1 for more help&lt;/Label&gt;
                              &lt;/StackPanel&gt;
                          &lt;/StackPanel&gt;
                      &lt;/Button.ToolTip&gt;
  
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;Image Source="Images/edit.png" Width="22" Height="22" Margin="2" /&gt;
                          &lt;Label VerticalAlignment="Center"&gt;_Edit&lt;/Label&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Button&gt;
                  &lt;!-- SEARCH --&gt;
                  &lt;Separator /&gt;
                  &lt;TextBox Name="txtSearchCriteria" 
                      MinWidth="300" Margin="5"
                      BorderThickness="1" BorderBrush="LightGray"
                      FontWeight="Normal" Foreground="Gray" Text="{Binding Path=SearchText}"&gt;
                  &lt;/TextBox&gt;
                  &lt;Button Name="btnSearch" Command="{Binding Path=SearchCommand}"&gt;
                      &lt;Button.ToolTip&gt;
                          &lt;StackPanel&gt;
                              &lt;Label FontWeight="Bold" Background="SteelBlue" Foreground="White"&gt;
                                  Search
                              &lt;/Label&gt;
                              &lt;TextBlock Padding="10" TextWrapping="WrapWithOverflow" Width="200"&gt;
                              Search a specific Customer.
                              &lt;/TextBlock&gt;
                              &lt;Line Stroke="SteelBlue" StrokeThickness="1" X2="200" /&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;Image Margin="2" Source="Images/find.png"/&gt;
                                  &lt;Label&gt;Press F1 for more help&lt;/Label&gt;
                              &lt;/StackPanel&gt;
                          &lt;/StackPanel&gt;
                      &lt;/Button.ToolTip&gt;
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;Image Source="Images/find.png" Width="22" Height="22" Margin="2" /&gt;
                          &lt;Label VerticalAlignment="Center"&gt;_Find&lt;/Label&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Button&gt;
                  &lt;Button Name="btnClearSearch" Command="{Binding Path=ClearSearchCommand}"&gt;
                      &lt;Button.ToolTip&gt;
                          &lt;StackPanel&gt;
                              &lt;Label FontWeight="Bold" Background="SteelBlue" Foreground="White"&gt;
                                  Search
                              &lt;/Label&gt;
                              &lt;TextBlock Padding="10" TextWrapping="WrapWithOverflow" Width="200"&gt;
                              Clear search results.
                              &lt;/TextBlock&gt;
                              &lt;Line Stroke="SteelBlue" StrokeThickness="1" X2="200" /&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;Image Margin="2" Source="Images/find.png"/&gt;
                                  &lt;Label&gt;Press F1 for more help&lt;/Label&gt;
                              &lt;/StackPanel&gt;
                          &lt;/StackPanel&gt;
                      &lt;/Button.ToolTip&gt;
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;Label VerticalAlignment="Center"&gt;_Clear Search&lt;/Label&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Button&gt;
                  &lt;!-- EXIT --&gt;
                  &lt;Separator /&gt;
                  &lt;Button Name="btnExit" Command="{Binding Path=ExitCommand}"&gt;
                      &lt;Button.ToolTip&gt;
                          &lt;StackPanel&gt;
                              &lt;Label FontWeight="Bold" Background="SteelBlue" Foreground="White"&gt;
                                  Start the application
                              &lt;/Label&gt;
                              &lt;TextBlock Padding="10" TextWrapping="WrapWithOverflow" Width="200"&gt;
                              Start the main application with the M-V-MV pattern.
                              &lt;/TextBlock&gt;
                              &lt;Line Stroke="SteelBlue" StrokeThickness="1" X2="200" /&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;Image Margin="2" Source="Images/dialog-information.png"/&gt;
                                  &lt;Label&gt;Press F1 for more help&lt;/Label&gt;
                              &lt;/StackPanel&gt;
                          &lt;/StackPanel&gt;
                      &lt;/Button.ToolTip&gt;
  
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;Image Source="Images/exit.png" Width="22" Height="22" Margin="2" /&gt;
                          &lt;Label VerticalAlignment="Center"&gt;_Exit&lt;/Label&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Button&gt;
              &lt;/ToolBar&gt;
          &lt;/DockPanel&gt;
          &lt;!-- LIST --&gt;
          &lt;DockPanel DockPanel.Dock="Top" MinHeight="30" Margin="0,0,0,5"&gt;
              &lt;Label&gt;Location:&lt;/Label&gt;
              &lt;ComboBox Name="cboLocation" Grid.Column="1" Grid.Row="4" 
                                  ItemsSource="{Binding Source={StaticResource LocationData}}"
                                  DisplayMemberPath="location_text" SelectedValuePath="location_value" 
                                  SelectedValue="{Binding Path=Location}" 
                                  HorizontalAlignment="Left" Width="175" Margin="4" /&gt;
          &lt;/DockPanel&gt;
          &lt;DockPanel Margin="5"&gt;
              &lt;DataGrid ItemsSource="{Binding Path=ListOfEmployees}" AutoGenerateColumns="False" IsReadOnly="True" 
                  Name="dgEmployee" SelectionMode="Single" SelectionUnit="FullRow" CanUserResizeColumns="True" 
                  SelectedItem="{Binding Path=SelectedEmployee}" GridLinesVisibility="Horizontal"&gt;
                  &lt;DataGrid.Columns&gt;
                      &lt;DataGridTextColumn Header="Employee ID" Width="SizeToCells"  MinWidth="125" Binding="{Binding EmployeeID}" /&gt;
                      &lt;DataGridTextColumn Header="First Name" Width="SizeToCells"  MinWidth="200" Binding="{Binding FirstName}" /&gt;
                      &lt;DataGridTextColumn Header="Last Name" Width="SizeToCells"  MinWidth="200" Binding="{Binding LastName}" /&gt;
                      &lt;DataGridTextColumn Header="Location" Width="SizeToCells"  MinWidth="125" Binding="{Binding Location}" /&gt;
                      &lt;DataGridCheckBoxColumn x:Name="Active" Header="Active" Binding="{Binding active}" MinWidth="75" /&gt;
                  &lt;/DataGrid.Columns&gt;
              &lt;/DataGrid&gt;
          &lt;/DockPanel&gt;
      &lt;/DockPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>