开发者

ListBox sort after items update, is Items.SortDescriptions binding INotifyPropertyChanged?

开发者 https://www.devze.com 2023-02-21 01:45 出处:网络
Big question update before continue: Is Items.SortDescription getting fired INotifyPropertyChanged events?

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.GetT开发者_如何转开发ype().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>();
    }
}

ListBox sort after items update, is Items.SortDescriptions binding INotifyPropertyChanged?


Not exactly sure what your problem is but you might need to add this before you add a GroupDescription.

MyListBox.Items.GroupDescriptions.Clear();

Now you added the cyclical comment that makes more sense. Why don't you bind to a PagedCollectionView rather than dispatching a sort to the control?

       PagedCollectionView itemListView = new PagedCollectionView(contacts);

        // Set the DataPager and ListBox to the same data source.
       itemListView.SortDescriptions.Clear();
       itemListView.SortDescriptions.Add(new SortDescription("State", ListSortDirection.Ascending));
       MyListBox.ItemsSource=itemListView;


AFAIK using GroupDescriptions like you're doing is the fastest way there is with a ListBox, unless you want to implement a control yourself. Are u running into performance issues right now? It should work fast enough.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号