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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:05:13+00:00 2026-05-21T09:05:13+00:00

Big question update before continue: Is Items.SortDescription getting fired INotifyPropertyChanged events? From the code

  • 0

Big question update before continue:
Is Items.SortDescription getting fired INotifyPropertyChanged events?

From the code of another question: Programmatically WPF listbox.ItemsSource update, I solved to use Items.Refres() that are slowing down execution…

But after I start to sort items, problem happened again.

As I tell in the other topic, I need to refresh the state of each changed user but that, need re-grouping the changed items of the listbox.

I get lost inputs like mouse and keyboard problem if I call cyclically a function to Clear() and re-add SortDescriptions.

Some code:

Threaded Timer:

    void StatusUpdater(object sender, EventArgs e)
    {
        ContactData[] contacts = ((Contacts)contactList.Resources["Contacts"]).ToArray<ContactData>();
        XDocument doc = XDocument.Load("http://localhost/contact/xml/status.php");
        foreach (XElement node in doc.Descendants("update"))
        {
            var item = contacts.Where(i => i.UserID.ToString() == node.Element("UserID").Value);
            ContactData[] its = item.ToArray();
            if (its.Length > 0)
            {
                its[0].UpdateFromXML(node);
            }
        }
        Dispatcher.Invoke(DispatcherPriority.Normal, new Action( () => { contactList.SortAndGroup(); } ));
    }

SortAndGroup() //It’s making my problem:

    public void SortAndGroup()
    {
        MyListBox.Items.SortDescriptions.Clear();
        MyListBox.Items.SortDescriptions.Add(new SortDescription("State", ListSortDirection.Ascending));
    }

ItemsSource:

public class Contacts : ObservableCollection<ContactData>, INotifyPropertyChanged
{
    private ContactData.States _state = ContactData.States.Online | ContactData.States.Busy;
    public new event PropertyChangedEventHandler PropertyChanged;
    public ContactData.States Filter 
    { 
        get { return _state; } 
        set 
        { 
            _state = value;
            NotifyPropertyChanged("Filter");
            NotifyPropertyChanged("FilteredItems");
        } 
    }
    public IEnumerable<ContactData> FilteredItems
    {
        get { return Items.Where(o => o.State == _state || (_state & o.State) == o.State).ToArray(); }
    }
    public Contacts()
    {
        XDocument doc = XDocument.Load("http://localhost/contact/xml/contactlist.php");
        foreach (ContactData data in ContactData.ParseXML(doc)) Add(data);
    }
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    } 
}

Source data class :

public class ContactData : INotifyPropertyChanged
{
    public enum States { Online = 1, Away = 2, Busy = 4, Offline = 128 }
    public event PropertyChangedEventHandler PropertyChanged;
    public int UserID 
    {
        get { return int.Parse(Data["UserID"]); }
        set { Data["UserID"] = value.ToString(); NotifyPropertyChanged("UserID"); }
    }
    public States State 
    {
        get { return (Data.Keys.Contains("State")) ? (States)Enum.Parse(typeof(States), Data["State"]) : States.Offline; }
        set 
        { 
            Data["State"] = value.ToString();
            NotifyStateChanged();
        }
    }
    public Dictionary<string, string> Data { get; set; }
    public void Set(string name, string value) 
    {
        if (Data.Keys.Contains(name)) Data[name] = value;
        else Data.Add(name, value);
        NotifyPropertyChanged("Data");
    }
    public Color ColorState { get { return UserStateToColorState(State); } }
    public Brush BrushState { get { return new SolidColorBrush(ColorState); } }
    public string FullName { get { return Data["Name"] + ' ' + Data["Surname"]; } }

    public System.Windows.Media.Imaging.BitmapImage Avatar
    {
        get 
        {
            return Utilities.Stream.Base64ToBitmapImage(Data["Avatar"]); 
        }
        set 
        { 
            Data["Avatar"] = Utilities.Stream.BitmapImageToBase64( value ); 
            NotifyPropertyChanged("Avatar"); 
        }
    }

    public ContactData() {}
    public override string ToString()
    {
        try { return FullName; }
        catch (Exception e) { Console.WriteLine(e.Message); return base.ToString(); }
    }
    Color UserStateToColorState(States state)
    {
        switch (state)
        {
            case States.Online: return Colors.LightGreen;
            case States.Away: return Colors.Orange;
            case States.Busy: return Colors.Red;
            case States.Offline: default: return Colors.Gray;
        }
    }

    public void UpdateFromXML(XElement xEntry)
    {
        var result = xEntry.Elements().ToDictionary(e => e.Name.ToString(), e => e.Value);
        foreach (string key in result.Keys) if (key != "UserID")
        {
            if (Data.Keys.Contains(key)) Data[key] = result[key];
            else Data.Add(key, result[key]);
            char[] property = key.ToCharArray();
            property[0] = char.ToUpper(property[0]);
            NotifyPropertyChanged(new string(property));
        }
    }

    public void NotifyStateChanged()
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("State"));
            PropertyChanged(this, new PropertyChangedEventArgs("ColorState"));
            PropertyChanged(this, new PropertyChangedEventArgs("BrushState"));
        }
    }

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            if (this.GetType().GetProperty(propertyName) != null)
            {
                if (propertyName != "State")
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                else NotifyStateChanged();
            }
        }
    }

    public static ContactData[] ParseXML(XDocument xmlDocument)
    {
        var result = from entry in xmlDocument.Descendants("contact")
        select new ContactData { Data = entry.Elements().ToDictionary(e => e.Name.ToString(), e => e.Value) };
        return result.ToArray<ContactData>();
    }
}

enter image description here

  • 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-21T09:05:14+00:00Added an answer on May 21, 2026 at 9:05 am

    I keep this for future if anyone need it…
    Now it’s run fluidly.

    I finally fixed it in this way:

            View = (ListCollectionView)CollectionViewSource.GetDefaultView(ContactData.ParseXML(doc));
            View.SortDescriptions.Add(new SortDescription("State", ListSortDirection.Ascending));
            View.SortDescriptions.Add(new SortDescription("FullName", ListSortDirection.Ascending));
            MyListBox.ItemsSource = View.SourceCollection;
    

    And in the update thread:

            while (IsStatusUpdateRunnig)
            {
                ContactData[] contacts = ((ContactData[])View.SourceCollection);//MyListBox.ItemsSource);
                XDocument doc = XDocument.Load("http://localhost/contact/xml/status.php");
                foreach (XElement node in doc.Descendants("update"))
                {
                    ContactData[] its = contacts.Where(i => i.UserID.ToString() == node.Element("UserID").Value).ToArray();
                    if (its.Length > 0)
                    {
                        its[0].UpdateFromXML(node);
                        Dispatcher.Invoke(new Action(() => { View.EditItem(its[0]); }));
                    }
                }
                Dispatcher.Invoke(new Action(() => { View.CommitEdit(); }));
                System.Threading.Thread.Sleep(2000);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question arises from the post Plain English Explanation of Big O . I
Update: Answers to this question helped me code the open sourced project AlicanC's Modern
BIG UPDATE: Ok I was getting the whole auto-increment point wrong. I though this
I got one big question. I got a linq query to put it simply
This is a big question, that I don't know how to start, so I
This question is a continuation of my previous question here zend models architecture (big
I read about Big-O Notation from here and had few questions on calculating the
Okay I concede that I didn't ask the question very well. I will update
I'm currently working on a quite big (and old, sigh) code base, recently upgraded
simple question maybe but can't find solution. I have button for refresh/update some contents

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.