Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5943933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:27:58+00:00 2026-05-22T16:27:58+00:00

I’ve created a sample project by following the instructions of this MSDN Walktrough .

  • 0

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:
enter image description here

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:
enter image description here

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:27:59+00:00Added an answer on May 22, 2026 at 4:27 pm

    @Freeze2046

    ICollectionView exposes the following methods to set the current item:

    • MoveCurrentToFirst
    • MoveCurrentToLast
    • MoveCurrentToNext
    • MoveCurrentToPrevious
    • MoveCurrentTo(object value)

    I typically find the last method the most useful for production code where I want to work with a selected item

        public Department SelectedItem
        {
            get { return _collectionView.CurrentItem as Department; }
            set { _collectionView.MoveCurrentTo(value); }
        }
    

    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.

    // retrieve the ICollectionView associated with the ObservableCollection
    _collectionView = CollectionViewSource.GetDefaultView(src);
    if (_collectionView == null) throw new NullReferenceException("_collectionView");
    
    //listen to the CurrentChanged event to be notified when the selection changes
    _collectionView.CurrentChanged += OnCollectionViewCurrentChanged;
    
    private void OnCollectionViewCurrentChanged(object sender, EventArgs e) {
        // whatever
    }
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.