The code below (c # winforms vs2010) allows you to add and delete checkedListBox lines with the associated properties Value1, Value2, Value3 (which in turn are associated with three label-mi). Can you please tell me how to fix the code so that after adding and deleting rows, they are sorted alphabetically in the checkedListBox while preserving the attached values? Is it possible to sort the lines with attached properties, for example, using the "Newtonsoft.Json" library (ie, press the button on the form, then use this library to upload to the file, sort in it, load from the file again into the checkedListBox)?
using System; using System.ComponentModel; using System.Linq; using System.Windows.Forms; namespace BindingsDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); checkedListBox1.DataSource = VM.DataSource; checkedListBox1.DisplayMember = "Name"; var nullValue = new Person { Name = "null", Value1 = string.Empty, Value2 = string.Empty, Value3 = string.Empty }; checkedListBox1.DataBindings.Add("SelectedItem", VM, "Focused", false, DataSourceUpdateMode.OnPropertyChanged, nullValue); label1.DataBindings.Add("Text", VM.DataSource, "Value1", false, DataSourceUpdateMode.OnPropertyChanged, nullValue); label2.DataBindings.Add("Text", VM.DataSource, "Value2", false, DataSourceUpdateMode.OnPropertyChanged, nullValue); label3.DataBindings.Add("Text", VM.DataSource, "Value3", false, DataSourceUpdateMode.OnPropertyChanged, nullValue); } private readonly PersonCollectionVM VM = new PersonCollectionVM(); private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e) { VM.Focused = checkedListBox1.SelectedItem as Person; } private void buttonAddNew_Click(object sender, EventArgs e) { var newval = VM.DataSource.AddNew(); newval.Name = "New name " + VM.DataSource.Count; newval.Value1 = newval.Name + "1"; newval.Value2 = newval.Name + "2"; newval.Value3 = newval.Name + "3"; VM.Focused = newval; } private void buttonRemove_Click(object sender, EventArgs e) { VM.DataSource.Remove(VM.Focused); } } internal class PersonCollectionVM : INotifyPropertyChanged { public PersonCollectionVM() { DataSource = new BindingList< Person >( new[] { new Person { Name = "Вася", Value1 = "1", Value2 = "2", Value3 = "3" }, new Person { Name = "Вася", Value1 = "12", Value2 = "22", Value3 = "32" } }.ToList() ); Focused = DataSource[ 0 ]; } private Person focused; public BindingList< Person > DataSource { get; set; } public Person Focused { set { if ( Equals( value, focused ) ) return; focused = value; OnPropertyChanged( "Focused" ); } get { return focused; } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } internal class Person { public string Name { set; get; } public string Value1 { get; set; } public string Value2 { get; set; } public string Value3 { get; set; } } }