Advertisement
DigitalMag

MVVM WinForms /* simple sample*/

Mar 27th, 2020
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Collections.ObjectModel;
  10. using System.Collections.Specialized;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.  
  15.  
  16.  
  17.     public partial class Form1 : Form
  18.     {
  19.         // ObservableCollection<int> collection = new ObservableCollection<int>() { 1, 2, 3 };
  20.         ObservableCollection<Setting> collection = new ObservableCollection<Setting>() {
  21.             new Setting{Key = "key1", Value1 = "value1"},
  22.             new Setting{Key = "key2", Value1 = "value2"},
  23.             new Setting{Key = "key3", Value1 = "value3"},
  24.             new Setting{Key = "key4", Value1 = "value4"}
  25.         };
  26.         Settings settings;
  27.  
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.         }
  32.  
  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.             // listBox1.DataSource = collection;
  36.             listBox2.DataSource = settings = new Settings(collection.ToArray());
  37.  
  38.             // listBox1.DisplayMember = "Key"; listBox1.ValueMember = "Value";
  39.             // listBox1.DataBindings.Add(".", collection, "");
  40.  
  41.             // collection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(collection_CollectionChanged);
  42.             settings.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(collection_CollectionChanged);
  43.  
  44.             //foreach (var item in collection) item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
  45.             foreach (var item in settings) item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
  46.            
  47.         }
  48.  
  49.         void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
  50.         {
  51.             // MessageBox.Show(e.PropertyName);
  52.  
  53.             var id = collection.IndexOf(sender as Setting);
  54.            
  55.            
  56.             collection[id] = sender as Setting;
  57.            
  58.         }
  59.  
  60.         void collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  61.         {
  62.             switch (e.Action)
  63.             {
  64.                 case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
  65.  
  66.                     MessageBox.Show(e.Action.ToString());
  67.                     break;
  68.                 case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
  69.  
  70.                     MessageBox.Show(e.Action.ToString());
  71.                     break;
  72.                 case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
  73.  
  74.                     MessageBox.Show(e.Action.ToString());
  75.                     break;
  76.                 case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
  77.  
  78.                     MessageBox.Show(e.Action.ToString());
  79.                     break;
  80.                 case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
  81.  
  82.                     MessageBox.Show(e.Action.ToString());
  83.                     break;
  84.                 default:
  85.                     break;
  86.             }
  87.  
  88.             if (sender == collection) { listBox1.DataSource = null; listBox1.DataSource = collection; }
  89.             else if (sender == settings) { listBox2.DataSource = null; listBox2.DataSource = collection; }
  90.  
  91.             // listBox1.DisplayMember = "Key"; listBox1.ValueMember = "Value";
  92.         }
  93.  
  94.         private void buttonAdd_Click(object sender, EventArgs e)
  95.         {
  96.             collection.Add(0);
  97.             settings.Add(0);
  98.         }
  99.  
  100.         private void buttonReplace_Click(object sender, EventArgs e)
  101.         {
  102.             collection[0] = 1;
  103.             settings[0] = 1;
  104.         }
  105.  
  106.         private void buttonRemove_Click(object sender, EventArgs e)
  107.         {
  108.             collection.RemoveAt(0);
  109.             settings.RemoveAt(0);
  110.         }
  111.  
  112.         private void buttonMove_Click(object sender, EventArgs e)
  113.         {
  114.             collection.Move(0, 1);
  115.             settings.Move(0, 1);
  116.         }
  117.  
  118.         private void buttonReset_Click(object sender, EventArgs e)
  119.         {
  120.             collection.Clear();
  121.             settings.Clear();
  122.         }
  123.  
  124.         private void buttonChange_Click(object sender, EventArgs e)
  125.         {
  126.             collection[0].Value1 = "111";
  127.         }
  128.  
  129.  
  130.     }
  131.  
  132.  
  133.  
  134.     public class Settings : ObservableCollection<Setting>
  135.     {
  136.         public Settings(IEnumerable<Setting> items) : base(items)
  137.         {
  138.             foreach (var item in items)
  139.             {
  140.                 item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
  141.             }            
  142.         }
  143.  
  144.         void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
  145.         {            
  146.             OnCollectionChanged(new NotifyCollectionChangedEventArgs(
  147.                 NotifyCollectionChangedAction.Replace,
  148.                 sender,
  149.                 sender,
  150.                 this.IndexOf(sender as Setting)));
  151.         }
  152.  
  153.  
  154.         public override event System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged;
  155.         protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  156.         {
  157.             // base.OnCollectionChanged(e);
  158.  
  159.             if (CollectionChanged != null)
  160.             {
  161.                 using (BlockReentrancy())
  162.                 {
  163.                     CollectionChanged(this, e);
  164.                 }
  165.             }
  166.  
  167.         }
  168.     }
  169.  
  170.  
  171.  
  172.     public class Setting : INotifyPropertyChanged
  173.     {
  174.  
  175.         string _value;
  176.  
  177.         public string Key { get; set; }
  178.         public string Value1
  179.         {
  180.             get { return _value; }
  181.             set
  182.             {
  183.                 _value = value;
  184.  
  185.                 if (this.PropertyChanged != null)
  186.                     PropertyChanged(this, new PropertyChangedEventArgs(
  187.                         System.Reflection.MethodBase.GetCurrentMethod().Name.Substring(4)
  188.                     )
  189.                 );
  190.             }
  191.         }
  192.  
  193.         public static implicit operator Setting(int a)
  194.         {
  195.             return new Setting { Key = "Key" + a, Value1 = a.ToString() };
  196.         }
  197.  
  198.         public override string ToString()
  199.         {
  200.            
  201.             return this.Key + ":" + this.Value1;
  202.         }
  203.  
  204.         public event PropertyChangedEventHandler PropertyChanged;
  205.     }
  206.  
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. // Form1.Designer.cs
  217.  
  218.  
  219. namespace WindowsFormsApplication1
  220. {
  221.     partial class Form1
  222.     {
  223.         /// <summary>
  224.         /// Требуется переменная конструктора.
  225.         /// </summary>
  226.         private System.ComponentModel.IContainer components = null;
  227.  
  228.         /// <summary>
  229.         /// Освободить все используемые ресурсы.
  230.         /// </summary>
  231.         /// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
  232.         protected override void Dispose(bool disposing)
  233.         {
  234.             if (disposing && (components != null))
  235.             {
  236.                 components.Dispose();
  237.             }
  238.             base.Dispose(disposing);
  239.         }
  240.  
  241.         #region Код, автоматически созданный конструктором форм Windows
  242.  
  243.         /// <summary>
  244.         /// Обязательный метод для поддержки конструктора - не изменяйте
  245.         /// содержимое данного метода при помощи редактора кода.
  246.         /// </summary>
  247.         private void InitializeComponent()
  248.         {
  249.             this.buttonAdd = new System.Windows.Forms.Button();
  250.             this.buttonRemove = new System.Windows.Forms.Button();
  251.             this.buttonMove = new System.Windows.Forms.Button();
  252.             this.buttonReplace = new System.Windows.Forms.Button();
  253.             this.buttonReset = new System.Windows.Forms.Button();
  254.             this.listBox1 = new System.Windows.Forms.ListBox();
  255.             this.buttonChange = new System.Windows.Forms.Button();
  256.             this.listBox2 = new System.Windows.Forms.ListBox();
  257.             this.SuspendLayout();
  258.             //
  259.             // buttonAdd
  260.             //
  261.             this.buttonAdd.Location = new System.Drawing.Point(620, 122);
  262.             this.buttonAdd.Name = "buttonAdd";
  263.             this.buttonAdd.Size = new System.Drawing.Size(147, 38);
  264.             this.buttonAdd.TabIndex = 0;
  265.             this.buttonAdd.Text = "Add";
  266.             this.buttonAdd.UseVisualStyleBackColor = true;
  267.             this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
  268.             //
  269.             // buttonRemove
  270.             //
  271.             this.buttonRemove.Location = new System.Drawing.Point(620, 208);
  272.             this.buttonRemove.Name = "buttonRemove";
  273.             this.buttonRemove.Size = new System.Drawing.Size(150, 66);
  274.             this.buttonRemove.TabIndex = 1;
  275.             this.buttonRemove.Text = "Remove";
  276.             this.buttonRemove.UseVisualStyleBackColor = true;
  277.             this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click);
  278.             //
  279.             // buttonMove
  280.             //
  281.             this.buttonMove.Location = new System.Drawing.Point(620, 328);
  282.             this.buttonMove.Name = "buttonMove";
  283.             this.buttonMove.Size = new System.Drawing.Size(169, 56);
  284.             this.buttonMove.TabIndex = 2;
  285.             this.buttonMove.Text = "Move";
  286.             this.buttonMove.UseVisualStyleBackColor = true;
  287.             this.buttonMove.Click += new System.EventHandler(this.buttonMove_Click);
  288.             //
  289.             // buttonReplace
  290.             //
  291.             this.buttonReplace.Location = new System.Drawing.Point(610, 39);
  292.             this.buttonReplace.Name = "buttonReplace";
  293.             this.buttonReplace.Size = new System.Drawing.Size(157, 55);
  294.             this.buttonReplace.TabIndex = 3;
  295.             this.buttonReplace.Text = "Replace";
  296.             this.buttonReplace.UseVisualStyleBackColor = true;
  297.             this.buttonReplace.Click += new System.EventHandler(this.buttonReplace_Click);
  298.             //
  299.             // buttonReset
  300.             //
  301.             this.buttonReset.Location = new System.Drawing.Point(400, 328);
  302.             this.buttonReset.Name = "buttonReset";
  303.             this.buttonReset.Size = new System.Drawing.Size(157, 55);
  304.             this.buttonReset.TabIndex = 4;
  305.             this.buttonReset.Text = "Reset";
  306.             this.buttonReset.UseVisualStyleBackColor = true;
  307.             this.buttonReset.Click += new System.EventHandler(this.buttonReset_Click);
  308.             //
  309.             // listBox1
  310.             //
  311.             this.listBox1.FormattingEnabled = true;
  312.             this.listBox1.Location = new System.Drawing.Point(38, 39);
  313.             this.listBox1.Name = "listBox1";
  314.             this.listBox1.Size = new System.Drawing.Size(159, 225);
  315.             this.listBox1.TabIndex = 5;
  316.             //
  317.             // buttonChange
  318.             //
  319.             this.buttonChange.Location = new System.Drawing.Point(400, 419);
  320.             this.buttonChange.Name = "buttonChange";
  321.             this.buttonChange.Size = new System.Drawing.Size(157, 56);
  322.             this.buttonChange.TabIndex = 6;
  323.             this.buttonChange.Text = "Change";
  324.             this.buttonChange.UseVisualStyleBackColor = true;
  325.             this.buttonChange.Click += new System.EventHandler(this.buttonChange_Click);
  326.             //
  327.             // listBox2
  328.             //
  329.             this.listBox2.FormattingEnabled = true;
  330.             this.listBox2.Location = new System.Drawing.Point(274, 30);
  331.             this.listBox2.Name = "listBox2";
  332.             this.listBox2.Size = new System.Drawing.Size(241, 238);
  333.             this.listBox2.TabIndex = 7;
  334.             //
  335.             // Form1
  336.             //
  337.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  338.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  339.             this.ClientSize = new System.Drawing.Size(815, 501);
  340.             this.Controls.Add(this.listBox2);
  341.             this.Controls.Add(this.buttonChange);
  342.             this.Controls.Add(this.listBox1);
  343.             this.Controls.Add(this.buttonReset);
  344.             this.Controls.Add(this.buttonReplace);
  345.             this.Controls.Add(this.buttonMove);
  346.             this.Controls.Add(this.buttonRemove);
  347.             this.Controls.Add(this.buttonAdd);
  348.             this.Name = "Form1";
  349.             this.Text = "Form1";
  350.             this.Load += new System.EventHandler(this.Form1_Load);
  351.             this.ResumeLayout(false);
  352.  
  353.         }
  354.  
  355.         #endregion
  356.  
  357.         private System.Windows.Forms.Button buttonAdd;
  358.         private System.Windows.Forms.Button buttonRemove;
  359.         private System.Windows.Forms.Button buttonMove;
  360.         private System.Windows.Forms.Button buttonReplace;
  361.         private System.Windows.Forms.Button buttonReset;
  362.         private System.Windows.Forms.ListBox listBox1;
  363.         private System.Windows.Forms.Button buttonChange;
  364.         private System.Windows.Forms.ListBox listBox2;
  365.     }
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement