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 8038497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:15:22+00:00 2026-06-05T03:15:22+00:00

I’m trying to solve a simple problem using LINQ, which I’m just learning. I

  • 0

I’m trying to solve a simple problem using LINQ, which I’m just learning.

I have a collection of strings, in this case representing serial ports, that will be displayed in a control, but must be ordered. The original collection is unsorted, and I don’t necessarily want to modify it, or make a copy of it. So, I created a property of type IEnumerable and bound it to a ComboBox.

This works great, the ComboBox has the correct contents in the correct order. However, if the original collection changes, either a) The ComboBox doesn’t get notified properly when the original collection changes, or b) the LINQ query isn’t being refreshed.

After trying some different things, I can’t see how the following code doesn’t work. I must be missing something.

There might be extra code here that is redundant… Anyway, the source followed by the XAML for the controls:

public partial class MainWindow : Window
{
    ObservableCollection<string> original = new ObservableCollection<string>();
    public ObservableCollection<string> OriginalList {
        get { return (original); }
    }

    private IEnumerable<string> _portList;
    public IEnumerable<string> PortList {
        get { return (_portList); }
    }

    public MainWindow() {
        InitializeComponent();

        original.Add("COM5");
        original.Add("COM1");
        original.Add("COM3");
        original.Add("COM4");
        original.Add("COM2");

        original.CollectionChanged += new NotifyCollectionChangedEventHandler(OriginalChanged);

        _portList = (
            from port in original
            orderby port ascending
            select port
        );

        DataContext = this;
    }

    private void AddPortButton_Click(object sender, RoutedEventArgs e) {
        original.Add("COM2.5");
    }

    void OriginalChanged(Object sender, NotifyCollectionChangedEventArgs args) {
        NotifyPropertyChanged("PortList");
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info) {
        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

The XAML:

    <ComboBox Name="SerialPortListBox" ItemsSource="{Binding PortList}" Width="100" />
    <ComboBox Grid.Row="1" Name="OriginalListBox" ItemsSource="{Binding OriginalList}" Width="100" Margin="0,5,0,0"/>
    <Button Grid.Column="1" Name="AddPortButton" Content="Add Port 2.5" Width="100" Margin="10,0,0,0" Click="AddPortButton_Click" />
  • 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-06-05T03:15:24+00:00Added an answer on June 5, 2026 at 3:15 am

    I think you will only get the change notifications you want by binding your ComboBox to an ObservableCollection. This class implements the interface INotifyCollectionChanged, which is what the innards of WPF rely on to notify the UI that updates are required.

    I see you are trying to get around this by implementing INotifyPropertyChanged for PortList, however this won’t work the way you want. That interface does not trigger the appropriate event to trigger the combobox to refresh. INotifyCollectionChanged tells the listener that a collection has changes (i.e; ‘Add’, ‘Remove’, ‘Move’, ‘Replace’, ‘Reset’), whereas INotifyPropertyChanged only indicates that some value has changed in the bound object. The ComboBox will not respond to an INotifyPropertyChanged event, in fact it’s probably not even subscribing to events of that type.

    So, either bind directly to the underlying datasource, or implement a 2nd ObservableCollection on top of that, rather than only an IEnumerable, which does not notify on change.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
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 parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put

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.