title: Data Binding with WPF - Update CollectionViewSource on Input
body: <p>I've created a sample project by following the instructions of this <a href="http://msdn.microsoft.com/en-us/library/gg197522%28v=vs.103%29.aspx" rel="nofollow noreferrer">MSDN Walktrough</a>. I've changed the sample a little bit and created two additional TextBox controls and a button.</p>
  
  <p>That's how it looks like:
  <img src="https://i.stack.imgur.com/c5KAX.png" alt="enter image description here"></p>
  
  <p>I've created a simple model using code first approach and a derived class of DbContext as a code generating item.</p>
  
  <p>After that I've used the Data Sources which were created:
  <img src="https://i.stack.imgur.com/NNAVG.png" alt="enter image description here"></p>
  
  <p>The "Name" - TextBox is ReadOnly. I want to allow the user to type in an ID and search for it, when the "Search" - Button is pressed. Thus, the "Name" - TextBox should be updated if the specified ID could be found. I don't care that much about the DataGrids, because I don't need them in my actual code.</p>
  
  <p>I've no idea how I can update the CollectionViewSource and validate the input, because the CollectionViewSource.View.CurrentItem property is read only.</p>
  
  <p><strong>EDIT</strong>: That's my (almost completely designer generated) code:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525" mc:Ignorable="d"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:my="clr-namespace:SchoolModel;assembly=SchoolModel" 
          Loaded="Window_Loaded"&gt;
  
      &lt;Window.Resources&gt;
          &lt;CollectionViewSource x:Key="departmentViewSource" d:DesignSource="{d:DesignInstance my:Department, CreateList=True}" /&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition/&gt;
              &lt;RowDefinition/&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;Grid DataContext="{StaticResource departmentViewSource}" Name="grid1"&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="Auto" /&gt;
                  &lt;ColumnDefinition Width="Auto" /&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="Auto" /&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Label Content="Department ID:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" /&gt;
              &lt;TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="departmentIDTextBox" Text="{Binding Path=DepartmentID, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" /&gt;
          &lt;/Grid&gt;
          &lt;Grid Grid.Row="1" DataContext="{StaticResource departmentViewSource}" Name="grid2"&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="Auto" /&gt;
                  &lt;ColumnDefinition Width="Auto" /&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="Auto" /&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Label Content="Name:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" /&gt;
              &lt;TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="nameTextBox" Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" /&gt;
          &lt;/Grid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  
  
  public partial class MainWindow : Window
  {
      private SchoolEntities _context = new SchoolEntities(@"data source=localhost\MSSQL2008R2;database=SchoolModel;integrated security=True;MultipleActiveResultSets=True");
      public MainWindow()
      {
          InitializeComponent();
      }
  
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          System.Windows.Data.CollectionViewSource departmentViewSource =
              ((System.Windows.Data.CollectionViewSource)(this.FindResource("departmentViewSource")));
  
          // Load is an extension method on IQueryable, defined in the System.Data.Entity namespace.
          // This method enumerates the results of the query, much like ToList but without creating a list. 
          // When used with Linq to Entities this method creates the entity instances and adds to the context.
          _context.Departments.Load(); // Load is defined in the System.Data.Entity namespace.      
  
          // After the data is loaded call the DbSet&lt;T&gt;.Local property to use the DbSet&lt;T&gt; as a binding source. 
          departmentViewSource.Source = _context.Departments.Local;
  
          var src = _context.Departments.Local;
          ICollectionView colelctionView = CollectionViewSource.GetDefaultView(src);
          colelctionView.Filter = new Predicate&lt;object&gt;(i =&gt; (i as Department).DepartmentID.ToString() == departmentIDTextBox.Text);
      }
  }
  </code></pre>