将 Windows 窗体迁移到 MVVM 模式

Jiale Xue - MSFT 41,271 信誉分 Microsoft 供应商
2024-03-14T07:29:47.07+00:00

嗨,团队,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Matrix
{
    public partial class Form1 : Form
    {
        int matrix;
        Form2 frm2;
        Dictionary<int, int> adicChecked = new Dictionary<int, int>();
        DataTable dt;

        public Form1()
        {
            InitializeComponent();
        }

        public void GetTable()
        {
            dt = GetData();
            dataGridView1.DataSource = dt;
        }

        /// <summary>
        /// Method for Datagrid Values
        /// </summary>
        /// <returns></returns>
        public DataTable GetData()
        {
            DataTable dt = new DataTable();

            for (int i = 0; i < matrix; i++)
            {
                dt.Columns.Add();
                DataRow row = dt.NewRow();
                dt.Rows.Add(row);
            }
            this.dataGridView1.Visible = true;
            return dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("Enter value greater than zero");
            }
            matrix = Convert.ToInt32(textBox1.Text);
            if (matrix > 0)
            {
                GetTable();
                if (frm2 != null)
                {
                    frm2.Close();
                }
                frm2 = new Form2(matrix);
                frm2.Show();
            }
            else
            {
                MessageBox.Show("Enter value greater than zero");
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            bool isChecked = true;
            int cellRow = e.RowIndex;
            int cellCoumn = e.ColumnIndex;
            if (dataGridView1.Rows[cellRow].Cells[cellCoumn].Value.ToString() == "True")
            {
                isChecked = false;
                dataGridView1.Rows[cellRow].Cells[cellCoumn].Value = isChecked;
            }
            else
            {
                isChecked = true;
                dataGridView1.Rows[cellRow].Cells[cellCoumn].Value = isChecked;

            }

            if (adicChecked.ContainsKey(cellRow))
            {
                adicChecked.Add(cellRow, cellCoumn);
            }
            List<string> list = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (dt.Rows[i][j].ToString() == "True")
                    {
                        list.Add("Row:" + i + " " + "column:" + j);
                    }
                }
            }

            frm2.CellClicked(cellRow, cellCoumn, isChecked,list);
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Matrix
{
    public partial class Form2 : Form
    {
        public delegate void MyDel(int cellRow,int cellCoumn);
        public event MyDel MyEvent;
        public int matrixValue { get; set; }
        public Form2(int mat)
        {
            InitializeComponent();
            matrixValue = mat;
            DataTable dt = GetData();
            dataGridView1.DataSource = dt;
        }
        /// <summary>
        /// Testing summary details
        /// </summary>
        /// <param name="cellRow"></param>
        /// <param name="cellCoumn"></param>
        /// <param name="isChecked"></param>
        /// <param name="list"></param>
        public void CellClicked(int cellRow, int cellCoumn, bool isChecked,List<string> list)
        {
            listBox1.Items.Clear();
            listBox1.Items.AddRange(list.ToArray());
            dataGridView1.Rows[cellRow].Cells[cellCoumn].Value = isChecked;
        }
        public DataTable GetData()
        {
            DataTable dt = new DataTable();
            for (int i = 0; i < matrixValue; i++)
            {
                DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
                dataGridView1.Columns.Add(chk);
                DataRow row = dt.NewRow();
                dt.Rows.Add(row);
            }
            this.dataGridView1.Visible = true;
            return dt;
        }
    }
}

我有一个项目是用两种形式开发矩阵 n*n,一种形式具有数据网格,另一种形式在单击形式一值后启用复选框,如切换的那样,现在我希望在不使用 Xaml 或 wpf 的情况下将其放入 MVVM,是否可以执行此操作??

我还想在MVVM模式中使用Events和委托作为使用的Case以及接口

我的项目的附件..

Note:此问题总结整理于:Migrate windows form to MVVM pattern

Windows 窗体
Windows 窗体
一组用于开发图形用户界面的 .NET Framework 托管库。
142 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 46,961 信誉分 Microsoft 供应商
    2024-03-14T07:44:51.2833333+00:00

    您可以在 Windows 窗体中使用 MVVM,并且可以像在 WPF 中一样链接属性和 ViewModel 命令。 有关使用 Windows 窗体实现 MVVM 的信息,请参阅此线程。 希望对您有所帮助

    如果回复有帮助,请单击“接受答案”并投赞成票。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助