Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Collections.ObjectModel;
- using System.Collections.Specialized;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- // ObservableCollection<int> collection = new ObservableCollection<int>() { 1, 2, 3 };
- ObservableCollection<Setting> collection = new ObservableCollection<Setting>() {
- new Setting{Key = "key1", Value1 = "value1"},
- new Setting{Key = "key2", Value1 = "value2"},
- new Setting{Key = "key3", Value1 = "value3"},
- new Setting{Key = "key4", Value1 = "value4"}
- };
- Settings settings;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- // listBox1.DataSource = collection;
- listBox2.DataSource = settings = new Settings(collection.ToArray());
- // listBox1.DisplayMember = "Key"; listBox1.ValueMember = "Value";
- // listBox1.DataBindings.Add(".", collection, "");
- // collection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(collection_CollectionChanged);
- settings.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(collection_CollectionChanged);
- //foreach (var item in collection) item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
- foreach (var item in settings) item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
- }
- void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- // MessageBox.Show(e.PropertyName);
- var id = collection.IndexOf(sender as Setting);
- collection[id] = sender as Setting;
- }
- void collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- switch (e.Action)
- {
- case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
- MessageBox.Show(e.Action.ToString());
- break;
- case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
- MessageBox.Show(e.Action.ToString());
- break;
- case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
- MessageBox.Show(e.Action.ToString());
- break;
- case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
- MessageBox.Show(e.Action.ToString());
- break;
- case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
- MessageBox.Show(e.Action.ToString());
- break;
- default:
- break;
- }
- if (sender == collection) { listBox1.DataSource = null; listBox1.DataSource = collection; }
- else if (sender == settings) { listBox2.DataSource = null; listBox2.DataSource = collection; }
- // listBox1.DisplayMember = "Key"; listBox1.ValueMember = "Value";
- }
- private void buttonAdd_Click(object sender, EventArgs e)
- {
- collection.Add(0);
- settings.Add(0);
- }
- private void buttonReplace_Click(object sender, EventArgs e)
- {
- collection[0] = 1;
- settings[0] = 1;
- }
- private void buttonRemove_Click(object sender, EventArgs e)
- {
- collection.RemoveAt(0);
- settings.RemoveAt(0);
- }
- private void buttonMove_Click(object sender, EventArgs e)
- {
- collection.Move(0, 1);
- settings.Move(0, 1);
- }
- private void buttonReset_Click(object sender, EventArgs e)
- {
- collection.Clear();
- settings.Clear();
- }
- private void buttonChange_Click(object sender, EventArgs e)
- {
- collection[0].Value1 = "111";
- }
- }
- public class Settings : ObservableCollection<Setting>
- {
- public Settings(IEnumerable<Setting> items) : base(items)
- {
- foreach (var item in items)
- {
- item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
- }
- }
- void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- OnCollectionChanged(new NotifyCollectionChangedEventArgs(
- NotifyCollectionChangedAction.Replace,
- sender,
- sender,
- this.IndexOf(sender as Setting)));
- }
- public override event System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged;
- protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- // base.OnCollectionChanged(e);
- if (CollectionChanged != null)
- {
- using (BlockReentrancy())
- {
- CollectionChanged(this, e);
- }
- }
- }
- }
- public class Setting : INotifyPropertyChanged
- {
- string _value;
- public string Key { get; set; }
- public string Value1
- {
- get { return _value; }
- set
- {
- _value = value;
- if (this.PropertyChanged != null)
- PropertyChanged(this, new PropertyChangedEventArgs(
- System.Reflection.MethodBase.GetCurrentMethod().Name.Substring(4)
- )
- );
- }
- }
- public static implicit operator Setting(int a)
- {
- return new Setting { Key = "Key" + a, Value1 = a.ToString() };
- }
- public override string ToString()
- {
- return this.Key + ":" + this.Value1;
- }
- public event PropertyChangedEventHandler PropertyChanged;
- }
- }
- // Form1.Designer.cs
- namespace WindowsFormsApplication1
- {
- partial class Form1
- {
- /// <summary>
- /// Требуется переменная конструктора.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Освободить все используемые ресурсы.
- /// </summary>
- /// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Код, автоматически созданный конструктором форм Windows
- /// <summary>
- /// Обязательный метод для поддержки конструктора - не изменяйте
- /// содержимое данного метода при помощи редактора кода.
- /// </summary>
- private void InitializeComponent()
- {
- this.buttonAdd = new System.Windows.Forms.Button();
- this.buttonRemove = new System.Windows.Forms.Button();
- this.buttonMove = new System.Windows.Forms.Button();
- this.buttonReplace = new System.Windows.Forms.Button();
- this.buttonReset = new System.Windows.Forms.Button();
- this.listBox1 = new System.Windows.Forms.ListBox();
- this.buttonChange = new System.Windows.Forms.Button();
- this.listBox2 = new System.Windows.Forms.ListBox();
- this.SuspendLayout();
- //
- // buttonAdd
- //
- this.buttonAdd.Location = new System.Drawing.Point(620, 122);
- this.buttonAdd.Name = "buttonAdd";
- this.buttonAdd.Size = new System.Drawing.Size(147, 38);
- this.buttonAdd.TabIndex = 0;
- this.buttonAdd.Text = "Add";
- this.buttonAdd.UseVisualStyleBackColor = true;
- this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
- //
- // buttonRemove
- //
- this.buttonRemove.Location = new System.Drawing.Point(620, 208);
- this.buttonRemove.Name = "buttonRemove";
- this.buttonRemove.Size = new System.Drawing.Size(150, 66);
- this.buttonRemove.TabIndex = 1;
- this.buttonRemove.Text = "Remove";
- this.buttonRemove.UseVisualStyleBackColor = true;
- this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click);
- //
- // buttonMove
- //
- this.buttonMove.Location = new System.Drawing.Point(620, 328);
- this.buttonMove.Name = "buttonMove";
- this.buttonMove.Size = new System.Drawing.Size(169, 56);
- this.buttonMove.TabIndex = 2;
- this.buttonMove.Text = "Move";
- this.buttonMove.UseVisualStyleBackColor = true;
- this.buttonMove.Click += new System.EventHandler(this.buttonMove_Click);
- //
- // buttonReplace
- //
- this.buttonReplace.Location = new System.Drawing.Point(610, 39);
- this.buttonReplace.Name = "buttonReplace";
- this.buttonReplace.Size = new System.Drawing.Size(157, 55);
- this.buttonReplace.TabIndex = 3;
- this.buttonReplace.Text = "Replace";
- this.buttonReplace.UseVisualStyleBackColor = true;
- this.buttonReplace.Click += new System.EventHandler(this.buttonReplace_Click);
- //
- // buttonReset
- //
- this.buttonReset.Location = new System.Drawing.Point(400, 328);
- this.buttonReset.Name = "buttonReset";
- this.buttonReset.Size = new System.Drawing.Size(157, 55);
- this.buttonReset.TabIndex = 4;
- this.buttonReset.Text = "Reset";
- this.buttonReset.UseVisualStyleBackColor = true;
- this.buttonReset.Click += new System.EventHandler(this.buttonReset_Click);
- //
- // listBox1
- //
- this.listBox1.FormattingEnabled = true;
- this.listBox1.Location = new System.Drawing.Point(38, 39);
- this.listBox1.Name = "listBox1";
- this.listBox1.Size = new System.Drawing.Size(159, 225);
- this.listBox1.TabIndex = 5;
- //
- // buttonChange
- //
- this.buttonChange.Location = new System.Drawing.Point(400, 419);
- this.buttonChange.Name = "buttonChange";
- this.buttonChange.Size = new System.Drawing.Size(157, 56);
- this.buttonChange.TabIndex = 6;
- this.buttonChange.Text = "Change";
- this.buttonChange.UseVisualStyleBackColor = true;
- this.buttonChange.Click += new System.EventHandler(this.buttonChange_Click);
- //
- // listBox2
- //
- this.listBox2.FormattingEnabled = true;
- this.listBox2.Location = new System.Drawing.Point(274, 30);
- this.listBox2.Name = "listBox2";
- this.listBox2.Size = new System.Drawing.Size(241, 238);
- this.listBox2.TabIndex = 7;
- //
- // Form1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(815, 501);
- this.Controls.Add(this.listBox2);
- this.Controls.Add(this.buttonChange);
- this.Controls.Add(this.listBox1);
- this.Controls.Add(this.buttonReset);
- this.Controls.Add(this.buttonReplace);
- this.Controls.Add(this.buttonMove);
- this.Controls.Add(this.buttonRemove);
- this.Controls.Add(this.buttonAdd);
- this.Name = "Form1";
- this.Text = "Form1";
- this.Load += new System.EventHandler(this.Form1_Load);
- this.ResumeLayout(false);
- }
- #endregion
- private System.Windows.Forms.Button buttonAdd;
- private System.Windows.Forms.Button buttonRemove;
- private System.Windows.Forms.Button buttonMove;
- private System.Windows.Forms.Button buttonReplace;
- private System.Windows.Forms.Button buttonReset;
- private System.Windows.Forms.ListBox listBox1;
- private System.Windows.Forms.Button buttonChange;
- private System.Windows.Forms.ListBox listBox2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement