I’ve created a sample project by following the instructions of this MSDN Walktrough. I’ve changed the sample a little bit and created two additional TextBox controls and a button.
That’s how it looks like:

I’ve created a simple model using code first approach and a derived class of DbContext as a code generating item.
After that I’ve used the Data Sources which were created:

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.
I’ve no idea how I can update the CollectionViewSource and validate the input, because the CollectionViewSource.View.CurrentItem property is read only.
EDIT: That’s my (almost completely designer generated) code:
<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">
<Window.Resources>
<CollectionViewSource x:Key="departmentViewSource" d:DesignSource="{d:DesignInstance my:Department, CreateList=True}" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid DataContext="{StaticResource departmentViewSource}" Name="grid1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Department ID:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<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" />
</Grid>
<Grid Grid.Row="1" DataContext="{StaticResource departmentViewSource}" Name="grid2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Name:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<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" />
</Grid>
</Grid>
</Window>
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<T>.Local property to use the DbSet<T> as a binding source.
departmentViewSource.Source = _context.Departments.Local;
var src = _context.Departments.Local;
ICollectionView colelctionView = CollectionViewSource.GetDefaultView(src);
colelctionView.Filter = new Predicate<object>(i => (i as Department).DepartmentID.ToString() == departmentIDTextBox.Text);
}
}
@Freeze2046
ICollectionView exposes the following methods to set the current item:
I typically find the last method the most useful for production code where I want to work with a selected item
It also exposes a CurrentChanged event that you can use to set up a handler to where you can do whatever you need (including validation) to react to the current item being changed.
I could say more about filtering, but I guess your just trying to work through some example code here and getting stuck on how to set the CurrentItem and react to it. Hopefully this will give you some ideas as to how to do that, but feel free to ask more questions if you need to.
Cheers,
Berryl